Many blogs have display most popular posts on their blog to make visitors stay longer on their posts, this is one of the way to attract more visitors to your blog. We actually can install a wordpress popular posts plugin to display our most popular posts, but if you don’t want to use plugin, you can simply hack your wordpress theme to do it manually.
Copy and paste the following code to the sidebar.php file, if you want to change the number of displayed posts, simply change the “5″ to your desired number.
<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>
Please note that the 5 most popular posts displayed is basing on the 5 most commented posts .
/images/logo.png)
/images/featured.png)
/images/recentposts.gif)
/images/ico-rss.gif)
/images/cat.png)
Great help there Eric. So many people will find this interesting. i have the code on my blog (in the header) and people often ask me how to do it on their blogs.
Do you know of a hack that does the same thing with meta page views instead of comments?
Hi
please include this into your query for more efficiency
WHERE post_type=’post’ AND post_status=’publish’
Thanks Vijay