wordpress recent posts show thumbnail

Matt picture Matt · May 21, 2013 · Viewed 14k times · Source

How can I show either a small thumbnail of a image or featured image, from each post In my recent posts custom widget

Here's my code:

<?php

include($_SERVER['DOCUMENT_ROOT'] . $root . 'blog/wp-load.php');

$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 3
));

?>

<h3 class="divider">Recent Blog Posts</h3>
<ul class="listing">
<?php foreach($recent_posts as $post) { ?>
    <li><a href="<?php echo get_permalink($post['ID']) ?>">
    <div><?php echo $post['post_title'] ?></div></a></li>
<?php } ?>  
</ul>

Answer

Bill picture Bill · May 21, 2013

You can use get_the_post_thumbnail

Usage:

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?>

Integrated with your code:

<?php foreach($recent_posts as $post) : ?>
    <li>
        <a href="<?php echo get_permalink($post['ID']) ?>">
            <?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
            <div><?php echo $post['post_title'] ?></div>
        </a>
    </li>
<?php endforeach; ?> 

Note: To enable Post Thumbnails, the current theme must include add_theme_support( 'post-thumbnails' ); in its functions.php file. See also Post Thumbnails.

Source(s)

Function Reference/get the post thumbnail

See also

Post Thumbnails
Function Reference/add theme support