WTFDWK

Wordpress: Show your “Most Popular Content” in your Sidebar

Categories: PHP  |  Written By: hoboballan

Do you wish to display your “Most Popular Content” in the sidebar on you Wordpress site..?

Well if so all you need to do is open up your sidebar.php file and add the below code to it and hay presto you now have the top 5 most commented posts on your blog.

If you wish to have more than 5 items all you need to do is change the 5 on line 3 to the number you wish to have.

<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> <?php echo $title ?> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>

Also if you don’t wish to show how many comments there are on each of the posts just delete:

{<?php echo $commentcount ?>}

Hat tip to ProBlogDesign for this great piece of code.

Tags: 

Leave a Reply

Back to the top

Looking for something?

Browse Posts By Category

Choose a category below to browse and subscribe to specific content.

Subscribe

Stay updated with WTFDWK via RSS or maybe you would like to share this post?