Friday, May 13, 2011

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.

Popular Posts:

    < ?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) { ?>
  • < ?php echo $title ?> {< ?php echo $commentcount ?>}
  • < ?php } } ?>

1 comment: