X

Display last months most commented posts as thumbnail

AdSanity review - expiration

This snippet will display last months most commented posts as thumbnails. Add the first snippet to the functions.php of your wordpress theme then add the second snippet where you wish to display the thumbnails. I currently use this snippet on http://wpsnipp.com after the third post if you wanted to see it in action.



Code Snippet:


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


Code Snippet:


<?php most_wps_popular_thumbnail(); ?>

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!