Post Pic

How to: Display your most popular content in your blog sidebar

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!

Related Posts

Related Posts

No related posts.

25 Responses

Mar 25 2009 10:58

thanks for your nice tips,but i have another question.How to display my blog’s monthly most popular content in my blog?

Mar 25 2009 10:58

Beats having to use a plugin. Thanks.

Mar 25 2009 11:27

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.

Mar 25 2009 11:35

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.

Mar 25 2009 15:13

Thank you for your sharing. It is advantage for me.

Mar 25 2009 16:05

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

Mar 25 2009 16:40

Thanks for the recipe. This most popular post is base on comment count. How about most popular post base on total view?

Mar 25 2009 20:10

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

Mar 26 2009 04:41

Another great recipe, all your recipes pretty much replace all the plugins I am using :-)

Mar 27 2009 22:07

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.

Apr 05 2009 06:11

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?

Apr 17 2009 03:02

nice, really nice!

Dec 17 2009 19:16

Anyway to make this work for wordpress.com ?

Dec 18 2009 14:48

Good one, except people dont really comment on my blog.

Jan 27 2010 03:47

Is there a way of making these show most viewed posts and not most commented posts?

Jan 27 2010 22:10

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:

Leave a Comment

* Name, Email, Comment are Required