WordPress Tip: How to create a 'Top Ten' list

Ed: This article is part of the 21 Days of WordPress Tips

Most bloggers have their favorite articles they’ve written. And while these may not be the most commented or most linked to, they are the best in displaying what the blogger writes about and his expertise. So what’s the best way to highlight these in a list on the home page or sidebar?

One option would be to hard code the links. However, as with most problems in hard coding anything, it’s going to be hard to change and will cause problems if you change the anything in the future.

Here’s an easy way to use custom fields and query_posts() to create your ‘Top Ten’ list:

1. Setup a custom field called ‘topposts’

1.a. You can do this by using the normal custom fields portion of the write/edit page. In this case, the key will be ‘topposts’ and the value will be ‘1’.

custom11

1.b. Or, if you use the More Fields plugin I reference in this post, you can just create a simple check box in the sidebar of your write/edit page.

custom2

2. Add the code to your theme

Once you’ve gone through and selected all the articles that you want in your ‘Top Ten’ list, we have to add the query_posts() call to your theme.

Thankfully, there is a custom field reference for query_posts(). Here’s the code:

<h3>My Top Ten Posts</h3>
<ul>
<?php
query_posts('meta_key=topposts&meta_value=Yes&showposts=10');
if (have_posts()) : while (have_posts()) : the_post();
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; ?>
</ul>

Just drop that code in on your sidebar or home page and you’ll have your top ten posts showing.

Comments

comments