How to Get Most Commented Posts with Thumbnail

There are so many way to attract visitors to stay on your WordPress blog, the one of them is show most popular post in your sidebar or footer, beside that fact in order to help your visitors finding your best content.

Let’s me share “How to get most commented posts along with their related thumbnail“, This trick need little bit code to be written on your themes files. I will show you how to do this. The first time, open your sidebar.php or footer.php or whatever files with your favorite editor. Then write down this code:

1
2
3
4
5
6
7
8
9
10
<?php $popular newWP_Query('orderby=comment_count&posts_per_page=5'); ?>
    <?php while ($popular->have_posts()) : $popular->the_post(); ?>
    <?php $justanimage = get_post_meta($post->ID,'thumbnail', true);
        if ($justanimage) { ?>
    <img src="<?php echo get_post_meta($post->ID, "Image", true); ?>" alt="<?php the_title(); ?>" />
    <?php } else { ?>
    <img src="<a href="/web/20120228235855/http://an-alternative-image.jpg"" rel="nofollow">http://an-alternative-image.jpg&quot</a>; alt="" />
    <?php } ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>

That I mean, on this code above, the thumbnail is taken from custom field “thumbnail”. You can change it depend on your thumbnail custom field. Thanks for Alex Denning for share this trick