I've looked everywhere and can't find an answer to this, I have duplicated the default recent posts widget (not customising core) - I've added styles etc to it, however, when I try to pull in the featured image for each post it just seems to pull in the same image for each post.

See below:

        <?php foreach ( $r->posts as $recent_post ) : ?>
            $post_title = get_the_title( $recent_post->ID );
            $title      = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
            <li><a href="<?php the_permalink( $recent_post->ID ); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><div class="related">

        <div class="related_img"><?php the_post_thumbnail(); ?></div>
        <div class="related_title"><?php echo $title ; ?></div>
        <div class="related_date"><i class="far fa-clock"></i> <?php echo get_the_date( '', $recent_post->ID ); ?></div>

        </div><div style="clear: both;"></div></a>
        <?php endforeach; ?>

How can I pull in the correct featured image?

1 Answer 1


the_post_thumbnail() will show the thumbnail of the current post in the Loop. Since you are using a custom foreach() loop instead of starting your own WordPress Loop, you can use this instead to specify which post you want:

<?php echo get_the_post_thumbnail( $recent_post->ID ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.