WordPress - How to List Recent Posts from One Category
At the bottom of the sidebar on my other pages like About, I have a list of my recent WordPress posts. Go ahead, go take a look and then come back - I’ll wait.
This is easily done by adding a loop to the sidebar and a query that locates posts from just one category.
Please note that this will only work with standard sidebars. If you are using widget ready sidebars you will want to install the Exec-PHP plugin and then add the code to one of the text widgets. Exec-PHP allows you to run PHP code in posts, pages and widgetized sidebars.
<php query_posts('category_name=wordpress&showposts=5'); ?>
<php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a> </li>
<?php endwhile; ?>
The main part of this is the standard WordPress loop that you will find on most of your template files.
<php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a> </li>
<?php endwhile; ?>
What differs is the query_posts code right before the loop.
<php query_posts('category_name=wordpress&showposts=5'); ?>
The query_posts code is telling WordPress to locate my last 5 posts in the WordPress category. The loop then runs the code to display them.
This is a great way to highlight the posts in your strongest or most popular category rather than just using a list of all the recent posts.
photo credit: drift-words
Related Posts:
WordPress - Collapsible Archives with the Clean Archives Reloaded Plugin
Wordpress - Enhance Search Results with the Search Excerpt Plugin
WordPress - How To Create a Static Front Page
How to Customize Gravatars in WordPress
How to Upgrade Wordpress Manually
Comments
RSS feed for comments on this post. | TrackBack URI















October 1, 2008
Thanks Kim, I have been thinking about implementing something like this on my blog. Will try it out.
Madhur Kapoor’s last blog post - Launching GameBojo.com - For the Gamers, By The Gamers
October 1, 2008
Yip, works like a treat. Thanks for another good tip.
Dave’s last blog post - Choosing the right ecommerce platform for your microbusiness
October 1, 2008
@Madhur - Thanks! Maybe you will find this useful on your new gaming site
I like your new photo and I hope you had a great birthday.
@Dave - Hi! I’m so glad you found it useful - and that you stopped by and left a comment.
October 1, 2008
Kim, you’re becoming quite the WordPress guru. When do you release your first theme?
Keep it up, I’m learning lots.
andymurd’s last blog post - Social Media Best Practices
October 1, 2008
Hi Andy - I know. That’s going to have to be next.
How about I’ll design one for you once you get your site on WordPress?
Thanks!
October 1, 2008
Yet another great tip… I guess, you are putting it altogether page by page for a new theme
Btw, if theme designing or revamping could also be one of your services (Like balkhis has a ‘pimp my blog’ as well as paid services)
Cheers,
Ajith
Ajith Edassery’s last blog post - Google bashing and Screw Google mentality!
October 2, 2008
Hi Ajith,
Well, Andy started the whole theme thing - I really don’t have time for it right now and it takes me forever … I fuss over every little thing. The tips are just things that I spent time trying to figure out and figured I should write them up and share them with others in way that’s easy to understand.
Thanks!
October 3, 2008
This is definitely something I want to add in my blog, although I am not sure where yet. But I have it bookmarked in my things to do! Thanks for the tip, great as always.
Kikolani - Poetry | Photography | Blogging Tips’s last blog post - The Blogging Process
October 3, 2008
Hi Kristi,
I’m glad you found it useful
For some reason I had the hardest time figuring out how to get this to work and once I did I wanted to share it with everybody.
My list of things to do/try in WordPress is way too long.
October 26, 2008
Hi,
I was trying something like this without a plugin. Thanks for the code, any heads up on how to do this with all the posts. like make it auto check the present category and display posts on it.?
Hope i am not asking for too much…lol
October 26, 2008
Hi,
I’m not entirely clear on what you’re asking. Do you mean show the recent posts by category based on the category of the article being viewed? Or show articles in the sidebar based on the category of the articles being viewed?
Either way this complicated without a plugin. You would need to use conditionals - if statements - if in this category do this, else do this, etc. There’s some information about this here in the Outside the Loop section http://codex.wordpress.org/Template_Tags/in_category and information about conditionals here http://codex.wordpress.org/Template_Tags/in_category
Perishable Press might be a good place to ask you question - Jeff is very knowledgeable about advanced code techniques http://perishablepress.com
The related posts plugin finds similar posts based on tags.
There is a related category plugin at http://playground.ebiene.de/400/related-posts-by-category-the-wordpress-plugin-for-similar-posts/
October 26, 2008
yea, i figured out about the if/else, I was wondering if there is a hook to get the current category from the post itself to make it work easier…lol
I was trying to display it on my homepage wordpress installation. Where i can show some 5 recent posts in that category…like photos…I think using your solution is a good way since I have only some 2-3 categories…
thanks for the quick response!
October 26, 2008
There might be a hook but I didn’t find one. If you only have a couple of categories, I do think my code example would be a lot easier