Categories
Wordpress Tutorials

Show Popular Posts in WordPress Sidebar

Many Bloggers show Popular Posts. Some one homepage, at the end of the post, on sidebar, etc. Popular Posts helps the reader to interact and spend more time on Blog. Reading more interesting and the popular articles of the Blog. Here is a quick code you can use to show Popular Posts in your Sidebar.

All you have to do is paste the following code in functions.php of your theme file.

<h2>Popular Posts</h2>
<ul>
< ?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) { ?>
	<li><a href="<?php echo get_permalink($postid); ?>" title="< ?php echo $title ?>">< ?php echo $title ?></a> {< ?php echo $commentcount ?>}</li>
< ?php } } ?></ul>

You may need to add some css styles to the code above to match your overall theme.Hope you found this small post useful. Leave a comment if you have anything to say !

Leave a Reply

Your email address will not be published.