WordPress Popular Posts Without Plugins
November 21st, 2009 | Author: Chip
I’ve been working on tweaking some themes, and I decided to remove all widgets and hardcode content boxes straight into the sidebar template file. So, here’s how to add a box with most popular (commented) posts:
<div>
<h2>Popular Content:</h2>
<div>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 8");
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></li>
<?php }}?>
</ul>
</div>
</div>
