wordpress recent posts with thumbnail

Following code snippet will display recent posts with thumbnail. You can add this code anywhere on your wordpress blog to display recent posts.

<h3>Recently Posts</h3>
<ul>
<?php
global $post;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0,'post_status'=>'publish'));
foreach($myposts as $post) :
setup_postdata($post);
?>
<li class="recent_post_item">
<?php
if(has_post_thumbnail()) {
echo '<a href="'.get_permalink().'">';
echo get_the_post_thumbnail($post->ID, array(80,80) );
echo '</a>';
} ?>
<h2><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></h2>
</li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
</ul>

Change array(80,80) if you want to change height and width of the image. right now image is 80px in width and 80px in height.

Leave a Reply

Your email address will not be published. Required fields are marked *