• Resolved griff701

    (@griff701)


    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)
  • Try:

    <?php $thumbnails = get_posts('numberposts=10&orderby=rand');
    foreach ($thumbnails as $thumbnail) :
    if ( !has_post_thumbnail($thumbnail->ID)) :  continue;
    else : echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
    echo '</a>';
    endif;
    endforeach;?>

    Thread Starter griff701

    (@griff701)

    Thank you for your time Esmi.

    Your code appears to do the same as the original piece – if there is a featured image it gets selected (all is well), if theres not an image the code doesn’t discard the post, it accepts it as having an ’empty’ featured image and gives me that.

    If it picks 10 posts without featured images I just get an empty widget to display 🙁

    Do you specifically want 10 images? If so, then that query isn’t going to get them for you. Why not pull all posts, then use a counter to stop the display after 10?

    Thread Starter griff701

    (@griff701)

    The trouble is I don’t know enough to actually know the right way to get what I want. (I cant believe there isn’t a simple plugin already for this, but I’ve searched for 2 months without finding anything)

    My thinking was this.

    Get a random post.
    Check it has an image.
    If it doesn’t, then discard the post.
    If it does, output the image.
    Repeat until the image count = 10 (or any number I need)

    Am I looking at this the wrong way?

    you could try to use the meta_key for thumbnail in you get_posts arguments; similar to http://wordpress.org/support/topic/display-the-post-only-if-thumbnail-is-set-up?replies=17#post-1769764

    untested example:

    <?php
    $thumbnails = get_posts(array('numberposts'=>10,'orderby'=>'rand','meta_key' => '_thumbnail_id')
    );
    foreach ($thumbnails as $thumbnail) {
    echo '<a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
    echo '</a>';
    }
    ?>

    Thread Starter griff701

    (@griff701)

    My god, thank you !!

    Thank you very, very much.

    A quick test and that appears to work perfectly.

    You’ve made my week 🙂 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Checking for featured images.’ is closed to new replies.