Post Pic

Get most commented posts with thumbnails

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!

Leave a Comment

* Name, Email, Comment are Required