0

I am trying to display wordpress image using it ID but the code i use is giving an error which is

Warning: count(): Parameter must be an array or an object that implements Countable in /var/www/wordpress/wp-content/themes/monstajamss/template-parts/content-article.php on line 10

This is my code

<div class="image large">
    <?php $thumb_id = get_post_thumbnail_id(get_the_ID()); $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true); if(count($alt)) ?>
    <img src="<?php the_post_thumbnail_url('large-size'); ?>" alt="<?php echo $alt; ?>">
</div>
13
  • 1
    Pretty self-explanatory - count expects an array...you are not passing an array. print_r() your $alt variable. Also, why are you using count()? What are you trying to do? Commented Sep 22, 2020 at 19:12
  • 1
    @disinfor To me, this has all the hallmarks of bits of code copied from various places and pieced without any understanding what they mean. They probably saw someone pass true into get_post_meta once and someone else use count to check the array they got returned before processing the results that got. Commented Sep 22, 2020 at 19:32
  • 1
    I would agree with that assessment! Commented Sep 22, 2020 at 19:33
  • 1
    Yes, it will work because of the reasons I explained in my answer - my answer was about more than just showing you the correct code :) - but you will also get a blank alt attribute so I also added a condition to check if it's empty in case you wanted to set a default alt value Commented Sep 22, 2020 at 19:36
  • 1
    Yeah, it will work when you remove count, because you were adding a conditional that count would evaluate to true - which it never would, because you were not passing an array to count. Also, count literally counts the array elements, so was the wrong approach if you wanted a conditional check before adding the alt information. Fluffy's answer basically adds a conditional that checks if the variable is set first. Commented Sep 22, 2020 at 19:36

1 Answer 1

3

You are just using get_post_meta incorrectly - it returns an array by default, but you are passing in true as the third parameter which changes it to return a single value only - see the Code Reference for get_post_meta.

Therefore your return value is not array, so you don't need to try count (which only works with arrays) or anything to evaluate an array of values before using it - it already has a single value so you just need to do the following:

<div class="image large">
    <?php $thumb_id = get_post_thumbnail_id(get_the_ID()); 
          $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true); 
     ?>
     <img src="<?php the_post_thumbnail_url('large-size'); ?>" 
         <?php 
          // only include the alt attrib if the alt text is not empty
          // (or you could set a default value, or whatever else you might want to do)
          if ($alt): ?>
             alt="<?php echo $alt; ?>" 
         <?php endif; ?>
     />
</div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.