DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

In order to help your visitors finding your best content, bloggers often display their most commented posts on their blog’s sidebar. In this recipe, I’ll show you how you can get most commented posts along with their related thumbnail.

Nothing hard with this recipe : Simply paste this code anywhere in your theme files, where you’d like the most commented posts to be displayed. Don’t forget to replace the custom field name by yours on line 5:

<?php $popular = new WP_Query('orderby=comment_count&posts_per_page=5'); ?>
	<?php while ($popular->have_posts()) : $popular->the_post(); ?>	
	<?php $justanimage = get_post_meta($post->ID, 'Image', true);
		if ($justanimage) { ?>
	<img src="<?php echo get_post_meta($post->ID, "Image", true); ?>" alt="<?php the_title(); ?>" />
	<?php } else { ?>
	<img src="http://an-alternative-image.jpg" alt="" />
	<?php } ?>
	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>

Thanks to Alex Denning for this nice trick!

2 Comments

  1. — “Don’t forget to replace the custom field name by yours on line 5:” —
    I don’t understand what I must replace? Please can someone help me?

  2. Thanks, Also I would like to add a specific sized thumbnail [48×48]. How do I do that?

    I added the following code to functions.php:

    add_image_size( ‘comment_post, 48, 48, true );

    What modifications are to be done in the above code to implement this?

Leave a Comment

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