Display Last Months Most Commented Posts as Thumbnail

Snippets by IsItWP

Are you looking for a way to show your most commented posts as the thumbnail? While there’s probably a plugin for this, we have created a quick code snippet that you can use to display last months most commented posts as thumbnail in WordPress.


All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function most_wps_popular_thumbnail(){

    $current_month = date('n');
    if($current_month==1){ $last_month=12; }else{ $last_month=$current_month-1; }

    $args = array(
	'posts_per_page' => 4,
	'monthnum'       => $last_month,
	'orderby'        => 'comment_count',
   <h1>Most popular snippets</h1> 
<?php query_posts($args); ?>
<?php  while (have_posts()) : the_post(); ?>
   <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'thumbnail' ); ?></a>
<?php endwhile; ?>

Add this code snippet in your theme’s file where you wish to display the posts thumbnails.

<?php most_wps_popular_thumbnail(); ?>

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: How to create a contact form in WordPress and 24 best WordPress multi-purpose themes.

Comments   Leave a Reply

  1. Perfect, just one small suggestion if I may. Change < ? to < ? php.  < ? short tag is convenient, but they're not secure or predictable. Not to mention that many ISPs have turned off short open tags and if they are the above won't work.

    Hopefully you don't mind me saying this.

    1. Hi Emil ,
      No problem at all wpsnipp was made to help people with wordpress so all input is great.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!