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

















