There was an error in the code example that has been corrected. The code will now work in a sidebar text widget.
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
Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter. If you are looking for Hosting, WordPress Theme, Newsletter or other recommendations, please view my detailed list.
121 Responses to “WordPress – How to List Recent Posts from One Category”
- 10 WordPress ‘HOW-TO’ to Give it the Quality it Deserves
- (Anti) Social-Lists 9/13/09 | (Anti) Social Development
- FAQPAL Blog
- 23 articles and resources to improve your Wordpress life! | JortK.nl
- WordPress: List Posts For A Category | The OS Quest
- 10 WordPress ‘HOW-TO’ to Give Your Blog the Quality it Deserves – Just a blog :)
- 10 Tips to Make Your WordPress Blog Outstanding - JustAnART
- Popular WordPress Tips for Beginners