July 25th, 2009
| Categories: Wordpress
| Tags: function, Popular Posts, sidebar, single.php, Widget, Wordpress
Hi everybody, you can show your most commented popular posts wtihout any plugin. It’s a simple code. Easy useable and appliable. We will edit siderbar.php or single.php. Where you want to show popular posts, you can edit any file. If you are ready, Lets begin…
Here is the code;
<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>
If you want to make a Popular Post Widget, you can use this code on sidebar.php or if you want to show this after post, you have to add this code after loop in single.php.
If you change the number “5” you can change Popular Posts Number
That’s All! Have a nice day.



Good tutorial… thanks ..
You’re Welcome!