
Blog posts aren’t all equals: Some are better than the others. In order to make visitors stay longer on your blog, what about giving them a quick access to your most commented posts? Here’s a nice code to do it easily.

Blog posts aren’t all equals: Some are better than the others. In order to make visitors stay longer on your blog, what about giving them a quick access to your most commented posts? Here’s a nice code to do it easily.
Just paste the following code on your sidebar.php file. To change the number of displayed posts, simply change the 5 on line 3.
<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>
Thanks to ProBlogDesign for this awesome recipe!
By the way, did you checked out WpClassipress? It is a great classified site 100% built within WordPress!
25 Responses
thanks for your nice tips,but i have another question.How to display my blog’s monthly most popular content in my blog?
Beats having to use a plugin. Thanks.
I’ve got a question:
I want to display 10 recent posts in my sidebar, but exclude the latest 10 that are on my home page.
So what I’m after is a way to display the recent posts from 11-20 ??? If that makes sense.
This is a nice piece of code. But, what can we do if we only want the most popular posts of the last month or the last 10 days (any random period of time). Can we alter somehow the SQL query to the database in order to specify time?
Thank you in advance,
asynadak.
Thank you for your sharing. It is advantage for me.
Another great recipe. Thanks
Off-topic – Please be aware because I saw that someone else commented using my name and email linking to a tires shop. I’m not responsible for that comments. Thanks
Thanks for the recipe. This most popular post is base on comment count. How about most popular post base on total view?
Thanks for the useful post and the mention! I’m tired of having to upgrade plugins and would rather just bake these into the theme or functions.php file instead.
~David
Another great recipe, all your recipes pretty much replace all the plugins I am using
I was always curious about making one. I have some posts that get read 10 times as much as others. This should help reduce the bounce rate.
Not sure why, but this code is doing some funky things to my blog. Its making the comments from popular post on the bottom of the list appear on every post.
Any ideas?
nice, really nice!
Anyway to make this work for wordpress.com ?
Good one, except people dont really comment on my blog.
Is there a way of making these show most viewed posts and not most commented posts?
Hi Jean!
Thats a nice recipe, i must say! I would like to know if post thumbnails can also be displayed along with the post title.
Trackbacks: