WordPress: Show popular posts based on comment count

Do you want to add a tabbed widget to show popular posts based on number of comments on your WordPress website? It is fairly easy to do with a custom WordPress post query. Just add the following code in sidebar.php or in any WordPress theme file where you want the list of popular posts to appear. Change the value in “showposts=5″ to your desired number.

<?php 
	$my_query = new WP_Query('orderby=comment_count&showposts=5');
	while ($my_query->have_posts()) : $my_query->the_post();

	if ( has_post_thumbnail() ): ?>
	<a href="<?php the_permalink(); ?>" title="<<?php the_title(); ?>" rel="bookmark">
			<?php // Use the image above post title
			echo get_the_post_thumbnail( $post->ID, 'thumbnail', array('class' => 'thumbnail') ); ?>
	</a>

	<?php endif; ?>

	<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
	<p><?php the_time('j M Y'); ?></p>
	<p><?php comments_popup_link('0 Comment', '1 Comment', '% Comments'); ?></p>

	<?php endwhile; ?>

	<?php wp_reset_query(); ?>

Premium WordPress Themes

Reliable Web Hosting

Smart Web Worker strongly recommends HostGator for your website/blog hosting. Sign up for web hosting at just $3.96/month. Use the coupon code "SMARTBLOGGER25" to get 25% discount on any hosting packages. Get an account with HostGator now!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>