Skip to main content

How to Create WordPress Popular Post with Thumbnail?

# Tips

Last week, I have tried some tweak to add WordPress Popular Post with Thumbnail module in the tabbed bar. This tweak will display numbers of popular post based on comments count in specific post. Meaning, the popularity of the post was determined by comment’s count. You can easily change number of post displayed in the module by altering the post number in the code.

In this post, I will show you the easy way to create this module. I have added thumbnail function by returning the key value of “thumbnail” into custom fields. You can learn more about custom field here.

So, enjoy the tutorial!

Easy Step to Create Popular Post with Thumbnail in WordPress

1. Create & specify your Div to add the popular post module. For me, I will use my current tabbed bar in Sidebar.

2. Copy and paste this code into specific Div Id/Class.

Popular Post (With Thumbnail)

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {setup_postdata($post);$postid = $post->ID;$title = $post->post_title;$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
<a href="<?php the_permalink() ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
<?php else: ?>
<a href="<?php the_permalink() ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;"  src="<?php bloginfo('template_url'); ?>/images/default_thumbnail.png" alt="<?php the_title(); ?>" /></a>
<?php endif; ?>
<b><a href="<?php echo get_permalink($postid); ?>" rel="bookmark" title="<?php echo $title ?>"><?php echo $title ?></a></b>
<?php } } ?>

3. If you prefer Popular Post module with no thumbnail. Just add this code:

Popular Post (No Thumbnail)

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {setup_postdata($post);$postid = $post->ID;$title = $post->post_title;$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<b><a href="<?php echo get_permalink($postid); ?>" rel="bookmark" title="<?php echo $title ?>"><?php echo $title ?></a></b>
<?php } } ?>

4. Save your themes and you’re done!. This is how my Popular Post with Thumbnail looks like =)
Easy Way to Create WordPress Popular Post with Thumbnail?