Post Pic

How to: Display the most commented posts of 2008

Happy new year! To get started with this new year, what about displaying a list of the 10 most commented posts of 2008 to your readers? That’s obviously a great way to give a second life to your old posts.

To display a list of your 10 most commented posts from 2008, simply paste the following code on your blog sidebar, or anywhere on your theme:

<h2>Most commented posts from 2008</h2>
<ul>
<?php
$result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '2008-01-01' AND '2008-12-31' ORDER BY comment_count DESC LIMIT 0 , 10");

foreach ($result as $topten) {
    $postid = $topten->ID;
    $title = $topten->post_title;
    $commentcount = $topten->comment_count;
    if ($commentcount != 0) {
    ?>
         <li><a href="<?php echo get_permalink($postid); ?>"><?php echo $title ?></a></li>
    <?php }
}
?>
</ul>

That's all! Hope you (and your readers!) like it :)
Best wishes to all of you and your families for 2009, have a happy and sucessfull year!

One Response

Aug 30 2011 01:50

How do I show him the most popular posts in a given category during the past week (remember that the week always starts on Monday).

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required