This might be a known code for many theme developers, if you aren’t aware about this one. You can see some websites displaying popular posts, those are nothing but posts ordered with most number of comments. Adding it to your theme is quite simple, just add this code
<?php $result = $wpdb->get_results("SELECT
comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count
DESC LIMIT 0 , 10");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>"
title="<?php echo $title ?>"><?php echo $title
?></a></li>
<?php } } ?>
The above code will display 10 posts with most number of comments in lists i.e between <li> </li> tags. I’m going off to sleep now, I’ll show you how to create a similar widget for sidebar later. Have a good night
Related posts:
