Checking for featured images.
-
Hi,
I’m hoping this is a very simple fix.
I’m using the code below to show a set of random featured image thumbnails in my side bar.
<?php $thumbnails = get_posts('numberposts=10&orderby=rand'); foreach ($thumbnails as $thumbnail) { if ( has_post_thumbnail($thumbnail->ID)) { echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">'; echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail'); echo '</a>'; } } ?>
As it stands, if a post doesn’t have a featured image this code will still include it in its list of source images to pick from – and return an ’empty’ image. If theres 5 posts without featured images, I can get back 5 blank lines and 5 pictures for my random set of 10
Is there an easy way to get the code to check the image isn’t empty? or to make sure it only chooses its images from posts that actually have a featured image?
Thank you.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Checking for featured images.’ is closed to new replies.