• I have searched high and low for a plugin or explaination via Google and the WordPress forums for an answer but cannot find any thing any where =(

    Using the new ‘featured image’ feature in WordPress, I want to display 4 different and random ‘featured image’ thumbnails on my homepage. I know there are plugins that will display 1 featured image thumbnail in the sidebar and I also know how to display 1 featured image thumbnail on my homepage but neither of these are exactly what I am looking to do. I need to be able to display 3, random ones which are selected from the featured images set in my posts.

    Is this at all possible?

    Gemma

Viewing 15 replies - 1 through 15 (of 21 total)
  • untested – try:

    <?php $args = array(
    'numberposts' => 4,
    'orderby' => 'rand',
    'meta_key' => '_thumbnail_id'
    );
    $random_thumbs = get_posts( $args );
    if( $random_thumbs ) foreach( $random_thumbs as $random_thumb ) {
    echo get_the_post_thumbnail($random_thumb->ID);
    }; ?>

    https://codex.www.ads-software.com/Function_Reference/get_the_post_thumbnail

    You could do this on your own, by looping through all the posts in the loop, and building an array of SRCs of featured images, and then output 3 random keys from the array. (And then, of course, rewinding the Loop when you’re done.)

    Thread Starter Pink Garden

    (@pinkgarden)

    Hi alchymyth

    Thank you for pointing me to that codex page as it almost had the exact code for what I needed sitting right there!

    <?php
    $thumbnails = get_posts('numberposts=5');
    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>';
    }
    }
    ?>

    This displays the 5 most recent posts with their own corresponding thumbnails and it links to the post. Which is what I want, but I’m actually after 5 random posts so that the older posts have a chance of being seen. Possible?

    Thread Starter Pink Garden

    (@pinkgarden)

    Chip, I wouldn’t even know where to begin with that one, lol!

    Gemma

    but I’m actually after 5 random posts so that the older posts have a chance of being seen. Possible?

    Just modify the get_posts() call to order by random posts. Change this:

    $thumbnails = get_posts('numberposts=5');

    …to this:

    $thumbnails = get_posts('numberposts=5&orderby=rand');

    Thread Starter Pink Garden

    (@pinkgarden)

    Thank you Chip, that works a charm!

    Gemma

    Thank you Chip, thats just what I needed as well ??

    Heres hoping this is an easy line of code ??

    <?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.
    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.

    ??

    Hello, I have 3 text posts, each with a featured image:

    When I use

    <?php the_content(); ?>
    <?php
    $thumbnails = get_posts('numberposts=5');
    foreach ($thumbnails as $thumbnail) {
    if ( has_post_thumbnail($thumbnail->ID)) {
    echo '<a>ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
    echo '</a>';
    }
    }
    ?>

    Inside ‘the loop’ I get the 3 posts, one after the other but each one has all 3 featured images showing underneath the content the text). Any ideas?

    What I’m actually trying to do is get just one to show at a time, at random, that changes on page refresh but I’m also trying to understand/learn PHP so maybe one step at a time ??

    Thanks a lot for any help anyone can give

    Can you re-post your code? It appears to have been mangled.

    <?php
    $thumbnails = get_posts('numberposts=5');
    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>';
    }
    }
    ?>

    underneath <?php the_content(); ?>

    Try:

    <?php
    $thumbnails = get_posts('numberposts=5');
    foreach ($thumbnails as $thumbnail) :
    setup_postdata($post);
    if ( has_post_thumbnail()) :?>
    <a href="<?php the_permalink();?>" title="<?php the_title_attribute( ); ?>"><?php the_post_thumbnail();?></a>
    <?php endif;
    endforeach;?>

    Thanks Esmi, that just shows 3 versions of the correct picture for each post! There’s something funny going on with the 3 thing here….

    Why 3 and not 5 given numberposts=5? Do you have another foreach loop going on before this one?

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Featured Image – Displaying 3 random images on the homepage?’ is closed to new replies.