How to Display the Most Popular Content in Your WordPress Sidebar

What is best way to keep your visitors entertained and make them stay longer on your blog? One way is to list the most popular content in your sidebar. The following code will display the top five posts with the most comments in your site. All you need to do is to paste the code below into your sidebar.php file and you are good to go.

<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>

Credit: WPRecipes

Share:
  • Twitter
  • Facebook
  • del.icio.us
  • LinkedIn
  • Technorati
  • Digg
  • NewsVine
  • Mixx
  • Google Bookmarks
  • Yahoo! Buzz
  • Live
  • Print
  • Add to favorites

Back to top ↑

Leave a comment

Name: (Required)

E-mail: (Required)

Website:

Comment:


Spam protection:
What is the sum of 5 + 9 ?