Home / comment / Display last months most commented posts as thumbnail
  • How to Display last months most commented posts as thumbnail

    display-last-months-most-commented-posts-as-thumbnail

    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.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    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();
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?php most_wps_popular_thumbnail(); ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    posts_per_page, the_title, the_permalink, query, have_posts, the_post, query_posts, wp, wp_reset_query,
  • Emil Uzelac

    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.

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