How to Show the Most Commented Posts on your WordPress Sidebar

Sep13

Are you still using a plugin to show the most commented posts on your WordPress blog? Discard that plugin and get more control. Read on to find out how to display the most commented posts in your WordPress sidebar or wherever you want to put it.

Starting now, I will limit my rants and I’ll just share you directly the code instead. You guys are big boys and girls and I know that you already know what to do but if you have a question, you can always contact me or let me know in the comments. Ask nicely and you shall be answered when I am not busy.

There are many ways on how to do this but I am only going to give you some examples. I hope you find it useful on your WordPress projects.

Method 1: Using the loop

Yep, you can do this on your regular wordpress loop. Grab the code below and test it now.

<ol>
<?php
$args = array(
	'posts_per_page' => 10, // specify the number of posts you want to list
	'orderby' => 'comment_count', // this only works for Version 2.9 and above but the question is are you not going to upgrade to the latest and greatest?
	'order' => 'DESC'
);
?>
<?php query_posts($args); ?>
	<?php while (have_posts()) : the_post(); ?>
	<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
	<?php endwhile;?>
</ol>

Method 2: Using get_posts

I usually prefer this method since you can use this on multiple loops without having so much trouble. Read more about get_posts.

<ol>
<?php
$args = array(
	'posts_per_page' => 10, 
	'orderby' => 'comment_count', 
	'order' => 'DESC'
);
	$recentposts = get_posts($args);
		foreach ($recentposts as $post) :
		setup_postdata($post);
	?>
	<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
	<?php endforeach; ?>
</ol>

Method 3: Showing Most Commented Posts by Category

Sometimes, we just want to show the most commented posts on certain categories. So how do we do that? Simple, just add the category_name parameter and you are all set. See the example below.

$args = array(
	'posts_per_page' => 10, 
	'orderby' => 'comment_count',
	'order' => 'DESC',
	'category_name' => 'Category Name' //rename 'Category Name' with your Category. Include the Spaces
);

Important

This stuff only works on versions of WordPress 2.9 and greater. But the thing is, why use the old outdated version of WordPress when you can make it easy on the latest and greatest version?

You can learn more about query_posts on the WordPress Codex.

Tags: , , , ,

Comments

Wanna share your thoughts? Leave a message
1
  1. It is working for me .. Good help

Find me on the Web

Other places you can find me

Daily Digest

Resource Sites for Inspiration

Extra Stuff

Represent!

Search this Website

Looking for something specific?