WordPress Tip: The Easy Way to Show a Popular Post List

Published April 7, 2008 12 Comments 4 Delicious Bookmarks

Some may recognize the above screenshot as the ‘Must Reads’ section from the blog of one of the best WordPress theme designers: Chris Pearson. In one of his most recent posts he explained how he uses categories. In this in-depth article, he mentions that categories are what powers the ‘Must Reads’ and ‘Worth a Look’ lists on the side of his site.

In my short tutorial here, I am going to show you how to easily do it for yourself.

First, we create another Loop and specify with the query_posts function that we only want to show a particular category. In this case, I have created a specialized category called ‘Popular’ and only use it for the purpose of this list.

<h3>Popular Articles</h3>
<ul>
	<?php query_posts('category_name=popular'); ?>
	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
	<?php the_excerpt(); ?></li>
	<?php endwhile; ?><?php endif; ?>
</ul>

After we add that code to any theme template file (I’ve added it to sidebar.php) all that is left is to add posts to the category ‘Popular’. From now on, just as long as a post is in that category, it will show in this list. All you need to do is add and remove posts from this category to alter what this list displays.

Also, in the code above, I just show the title and except, but you are free to customize it any way you see fit. Just about anything that can be used within the regular Loop is fair game. Here is a list of tags that should work for you.

Told you it was easy.

12 Comments

  1. April 15th, 2008 @ 5:01 am

    [...] explains how to create a category loop to display those posts in your sidebar. Chris Pearson has also written a great post about the ideal [...]

  2. July 18th, 2008 @ 11:31 am

    Marita said:

    Hello,
    Do you have any tips on how to reset the “most popular” articles in wordpress every week, every day or every month?
    Since everyone always clicks on the most popular article, this list never changes.

  3. July 18th, 2008 @ 11:42 am

    @Marita – I can’t think of any way to “reset” the list, but what you can do is set a limit as to the number of posts you want to display (e.g. 3) and everytime you add a new one to the list, the bottom will drop off, etc. This will give you a rolling list that will change each time you add a new post as “popular”.

  4. July 18th, 2008 @ 5:20 pm

    Marita said:

    Thanks Chris for the tip; not quite sure if I understand you correctly: currently I have the limit set to 6, but it never changes because these six already have hundreds of pageviews and when I add a new article it starts at one. Did you mean actually changing the number from lets say 6 to 3 and then back, so it start counting again from 0?

    Thanks for all your help!

  5. July 18th, 2008 @ 6:46 pm

    @Marita – Are you using a plugin to generate your popular posts list? I ask because in my example, pageviews have nothing to do with it at all…

  6. July 18th, 2008 @ 7:09 pm

    Marita said:

    I assume it’s a plugin that counts pageviews – how else could popular posts be determined (besides counting comments)?

  7. July 18th, 2008 @ 7:43 pm

    I think you are a little confused here Marita…

    Take a look through my tutorial and you can see that I don’t count pageviews or comments. This is a manual list that is displayed when you add a post to the “Popular” category.

  8. July 18th, 2008 @ 8:04 pm

    Marita said:

    I don’t know how to explain it any differently – but thanks anyway :)

  9. November 4th, 2008 @ 12:59 pm

    Do you have an example of the code to limit the number of posts displayed?

  10. November 4th, 2008 @ 2:21 pm

    Chester – Take a look here: http://codex.wordpress.org/Template_Tags/query_posts#Offset_Parameter – it explains that all you need to do is add &showposts=10 to the query_posts function (line 3 above) to show only the first 10 posts.

    <?php query_posts('category_name=popular&showposts=10'); ?>
  11. November 4th, 2008 @ 3:44 pm

    Perfect – thank you!

  12. October 12th, 2009 @ 12:37 pm

    Aery said:

    Hi,
    Thanks for the exellent tip. I was looking for the same.

    But since my permalinks are domain/category/post-title and I am planning to use ‘Popular’ as tag, Can you please tell how to use the wp-loop now?

    I mean what modifications I need to do now for it?

    Regards,
    Aery

Leave your Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get your own Gravatar