Display Popular Posts In Your WordPress Blog’s Sidebar Without Using Any Plugin

You can highlight most popular content of your WordPress site in your sidebar. Copy-Paste the following code in your theme’s sidebar.php file:

<h2>Popular Posts</h2>
<?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) {
$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 } } ?>

It displays most popular posts on your site’s sidebar.

For changing the number of posts to display in your sidebar:

Look-up for this line in the above code block: FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5″); and Replace 5 with the number of posts you like to display.

Easiest Way for Displaying both Popular and Unpopular Posts in your Sidebar:

Now easiest way to do this (display popular posts) and many other similar things like displaying both popular and unpopular posts, knowing the number of post hits is to use Most and Least Read Posts Widget Plugin

Download from the link, activate it and start using it by placing its widgets in your sidebar. It gives you two different widgets one shows most read posts and the other shows least read posts and also it counts your post hits both on sidebar and in your dashboard All Posts list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.