Tuesday, August 3, 2010

TutZone- Tutorials, Design news and Inspiration

TutZone- Tutorials, Design news and Inspiration

Link to TutZone

How To Display Most Popular Posts in WordPress

Posted: 03 Aug 2010 07:25 AM PDT


There comes many plugins who can display the Most Popular Posts from your blog. But then using too many plugins slow down your blog. To help you quickly and easily display Most Popular Articles here is a simple tutorial for you. Hope this helps.

popular posts

One Step Tutorial:

Place below code where you want to display your Most Popular Posts:

<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>

Just change the 5 in line 3 to change the number of displayed popular posts. That’s it.

No comments:

Post a Comment