WordPress.org

Ready to get started?Download WordPress

Forums

Featured Image - Displaying 3 random images on the homepage? (22 posts)

  1. Gemma Wild
    Member
    Posted 3 years ago #

    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

  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    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);
    }; ?>

    http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

  3. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

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

  4. Gemma Wild
    Member
    Posted 3 years ago #

    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?

  5. Gemma Wild
    Member
    Posted 3 years ago #

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

    Gemma

  6. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    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');
  7. Gemma Wild
    Member
    Posted 3 years ago #

    Thank you Chip, that works a charm!

    Gemma

  8. griff701
    Member
    Posted 2 years ago #

    Thank you Chip, thats just what I needed as well :)

  9. griff701
    Member
    Posted 2 years ago #

    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.

    :)

  10. katymoon
    Member
    Posted 2 years ago #

    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

  11. esmi
    Forum Moderator
    Posted 2 years ago #

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

  12. katymoon
    Member
    Posted 2 years ago #

    <?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>';
    }
    }
    ?>
  13. katymoon
    Member
    Posted 2 years ago #

    underneath <?php the_content(); ?>

  14. esmi
    Forum Moderator
    Posted 2 years ago #

    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;?>
  15. katymoon
    Member
    Posted 2 years ago #

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

  16. esmi
    Forum Moderator
    Posted 2 years ago #

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

  17. katymoon
    Member
    Posted 2 years ago #

    My whole index.php is

    [Code moderated as per the Forum Rules. Please use the pastebin]

  18. katymoon
    Member
    Posted 2 years ago #

    If I add a fourth post - I'm now seeing 4 versions of the same image for each post.

  19. katymoon
    Member
    Posted 2 years ago #

    I'm gonna start again with this with a different approach so don't worry about trying to help :) Thank you

  20. successprovince01
    Member
    Posted 2 years ago #

    instead of having to mess with some template html coding, which I of course know nothing about, can't one use a plugin that will perform this function?
    I want to display about 6 random thumbnails of my recent posts in my homepage- 3 columns and 2 rows.Somebody please, Help!! I want it to look something like the one on this website: http://alilochhead.com/

  21. successprovince01
    Member
    Posted 2 years ago #

    I hope someone here will assist eventually....

  22. tyskkvinna
    Member
    Posted 2 years ago #

    I want to do exactly this feature, but with pages rather than posts. When I try to manipulate it in various ways it ends up bungled. Any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic