-
WordPress: Show your “Most Popular Content” in your Sidebar
Date: Apr 17, 2009 | Categories: Wordpress | Written By: Mike Ballan
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><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"> <?php echo $title ?></a> {<?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: Wordpress