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>
countexpects an array...you are not passing an array.print_r()your$altvariable. Also, why are you usingcount()? What are you trying to do?trueinto get_post_meta once and someone else usecountto check the array they got returned before processing the results that got.count, because you were adding a conditional thatcountwould evaluate to true - which it never would, because you were not passing an array tocount. Also,countliterally counts the array elements, so was the wrong approach if you wanted a conditional check before adding thealtinformation. Fluffy's answer basically adds a conditional that checks if the variable is set first.