WordPress – How to List Recent Posts from One Category
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
Related Posts:
48 Responses to “WordPress – How to List Recent Posts from One Category”
Trackbacks/Pingbacks
- 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









October 1, 2008 at 2:01 pm
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 at 5:24 pm
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 at 5:46 pm
@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 at 6:18 pm
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 at 6:22 pm
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 at 10:04 pm
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 at 12:19 am
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 at 4:48 pm
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 at 7:40 pm
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 at 12:57 pm
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 at 4:42 pm
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 at 6:28 pm
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 at 7:03 pm
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 :-)
March 17, 2009 at 10:38 pm
Hi, I so need this feature. I’m trying it out in a text widget w/ exec_php, but getting a run time error that apparently means there’s something not right about the php”
Parse error: syntax error, unexpected T_ENDWHILE in /wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 6
Thought I’d let you know …
March 17, 2009 at 11:18 pm
I think there are more ENDWHILE’s down the line on my page and that’s why it’s not working. However, I got it to work using the following:
<a href=”">
Elke´s last blog post – A Simplified Gallery with Pixelpost
March 17, 2009 at 11:20 pm
didn’t take the code I posted
but it’s here wordpress.org/support/topic/167045
Elke´s last blog post – A Simplified Gallery with Pixelpost
March 18, 2009 at 10:21 am
Hi Elke – Additional endwhiles can definitely cause the problem. I use the same exact code in my example on this site so I do know that it works. I’m glad you found an alternative solution in the codex.
March 25, 2009 at 6:12 pm
Hi Elke –
The code works but the error was mine in the above block – I am missing the opening ? with <?php in the first two lines. Once that is corrected it works fine in a widget.
March 21, 2009 at 3:05 am
same error for me too … I used the same solution as above, but removed the offset so I get last five posts under the category … also if you use her solution with offset of one, you need to have more than one post in category or you get a blank
so anyway as I got the same error you may want to explore it … I was using it in a sidebar text widget using the proposed php solution if that helps
Don´s last blog post – Kudo’s to Local Computer Repair Company: Grand Prix Computer
March 21, 2009 at 10:26 am
Hi Don – I’ll test it again in a widget. Mine is in the sidebar manually – I think I tested it in a widget when I first wrote the article but right now I can’t remember. Thanks!
March 25, 2009 at 6:12 pm
Hi Don,
The code works but the error was mine in the above block – I am missing the opening ? with <?php in the first two lines. Once that is corrected it works fine in a widget.
May 15, 2009 at 9:56 am
Many thanks Kim, This was really helpful.
Tasin
Tasin Reza´s last blog post – How to display error message on forms.
May 15, 2009 at 10:55 am
Hi Tasin – You’re welcome. I’m glad the article was useful.
July 27, 2009 at 1:10 pm
hey, i tried this code and it seemed to work great, but once I logged out of the admin section and just viewed the site the recent post tab was empty. not sure what happened.
matthew´s last blog ..Camp Kingdom!
July 27, 2009 at 8:39 pm
Hi Matthew,
I would need to see an example of the code – it can be placed in the comments between the
tags. If you are using it in a text based widget, are you using the Exec-php plugin? And is the category name correct?July 27, 2009 at 1:11 pm
also, is it possible to add two post categories instead of just one?
matthew´s last blog ..Camp Kingdom!
July 27, 2009 at 8:46 pm
Hi Matthew,
To display more than one category you want to use an array and use the category id numbers rather than the category names.
Something like
query_posts(array('category__and'=>array(1,3),'showposts'=>2,
'orderby'=>title,'order'=>DESC));
July 28, 2009 at 2:25 pm
I am editing the code to the sidebar.php. here is the code:
<a href=”">
var countries=new ddtabcontent(“tabs”)
countries.setpersist(false)
countries.setselectedClassTarget(“link”)
countries.init()
again, it shows up when I’m logged in as the admin, but otherwise it doesn’t work. I’ll use to array code and see what happens there. Who knows, maybe it will fix it. At any rate, thanks for your help. I’ll let you know what happens.
matthew´s last blog ..Camp Kingdom!
July 28, 2009 at 2:26 pm
Hm, let’s see if this works.
<a href="">
var countries=new ddtabcontent("tabs")
countries.setpersist(false)
countries.setselectedClassTarget("link")
countries.init()
matthew´s last blog ..Camp Kingdom!
July 28, 2009 at 2:30 pm
i apologize, apparently I don’t know how to embed code. I’ll try once more. I took out all of the . Hopefully this works.
div id=”tabsWrap”
p id=”tabs”
a href=”#” rel=”tab1″ class=”selected” ?php _e(‘Recent Posts’); ? /a
a href=”#” rel=”tab2″ ?php _e(‘Recent Comments’); ? /a
/p
ul id=”tab1″
?php query_posts(‘category_name=kingdom-concepts&showposts=5′); ?
?php while (have_posts()) : the_post(); ?
li a href=”?php the_permalink(); ?”
?php the_title(); ?
/a /li
?php endwhile; ?
/ul
ul id=”tab2″
?php include (TEMPLATEPATH . ‘/simple_recent_comments.php’); ? ?php if (function_exists(’src_simple_recent_comments’)) { src_simple_recent_comments(3, 100, ”, ”); } ?
/ul
script type=”text/javascript”
var countries=new ddtabcontent(“tabs”)
countries.setpersist(false)
countries.setselectedClassTarget(“link”)
countries.init()
/script
/div
matthew´s last blog ..Camp Kingdom!
July 29, 2009 at 8:12 am
Hi Matthew – I emailed you and it seems that you got the array to work.
August 13, 2009 at 1:10 pm
friends in my home.php if i use
then the page numbers plug in not working,
page numbers are showing but if click on 2nd page it showing againg 1st page
August 13, 2009 at 4:32 pm
Hi Vamsi – I’ve learned that query posts can be very fussy and cause that problem. You will want to use something like this
< ?php
{
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("category_name=videos-only&posts_per_page=6&paged=$page");
}
while (have_posts()) : the_post();
?>
October 9, 2009 at 10:25 am
Hi, if you want to use a “recent post function” for only one particular category here is a my example :
<?php
// example here for the 7 most Recent posts for the category 5
$output = '';
$before = '';
$after = '';
query_posts('showposts=7&cat=5');
while (have_posts()) : the_post();
$post_title = stripslashes($post->post_title);
$permalink = get_permalink($post->ID);
$output .= $before .'' . htmlspecialchars($post_title). '';
$output .= $after;
endwhile;
echo $output;
?>
Instead you have got this function mdv_recent_posts(number of post);
but it doesn’t work with only one category exclusion …
Mecanographik´s last blog ..links for 2009-10-08
October 9, 2009 at 1:42 pm
Hi – Thanks for the code example. I’m going to try it out. :-)
October 16, 2009 at 6:22 pm
Hi Kim,
Thanks for such wonderful trick.
just One Question – How do I implement it with Tags?
I mean, like you have shown latest posts by categories, I would like to do same for my new theme with the help of Tags.
Can you please take out 2 minutes to figure this one out. I am sure you can.
Regards.
Aery´s last blog ..Best 10 Gadgets for Gmail that are Damn Cool and Useful
October 17, 2009 at 9:37 am
Hi – I’m not sure. I would have to work on it. There is a plugin that might help you out though.
http://sudarmuthu.com/wordpress/posts-by-tag
January 22, 2010 at 4:47 pm
Kim,
Great stuff! I did a quick search on Google and found your post on the first page and it did the trick. (search: list category posts wordpress) <– Just thought you would like to know :)
Anyways I'm very grateful that you put this up!
- Shawn
January 22, 2010 at 11:03 pm
Hi Shawn – Thanks! There are a couple of search phrases that give me good results. And I’m really glad the article helped you out :-)
February 9, 2010 at 6:36 pm
Works great, thank you!
February 12, 2010 at 6:21 am
Hello Kim,
I was confused with the code (bad memory), I googled it and see where I landed :)
Thanks
Arun Basil Lal´s last blog ..Bandwidth Monitoring Tool for Windows Vista and Seven
February 12, 2010 at 7:51 am
Hi Arun – I’m glad to know I’m being found through google ;-)
February 15, 2010 at 10:56 pm
Thank you for the code. I used it on a client’s site and it worked perfectly!
Roger´s last blog ..The most compelling reason to hire outside help in planning and implementing a social media strategy
February 16, 2010 at 6:10 am
Hi Roger – Thanks – I’m glad it helped you out.
February 22, 2010 at 8:26 am
works like wonder on my blog, thanks! :D
February 22, 2010 at 10:42 am
Hi Tigris – Great! I’m glad that it worked for you.
February 24, 2010 at 11:45 am
Helped me a lot! Thank you very much! :D
February 24, 2010 at 1:53 pm
Hi Marc – Great! Thanks for letting me know.