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.

Categories (by Drift Words)

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

It's Time!

Start your WordPress website project now!

Contact Me! >>


Keep Reading:
WordPress Helper: A Useful Firefox Extension
How to Link NextGEN Gallery Images to an Individual Page or Post in WordPress
Converting a Static Website to WordPress: Mary Shelley and Frankenstein Part 2
How to Update the Copyright Date on your WordPress Blog Automatically


    • says

      Ok, my question is do you get better performance by adding this to the theme versus using a plugin (per your above). I’m assuming that bypassing a plugin/widget is the most direct and fastest way to pull this part of your page.

      • says

        Hi Derick – I think a simple piece of code is almost always a more efficient solution than a plugin. I don’t know if the performance will be better – it depends on a lot of factors such as what other plugins are installed, how many sidebar widgets do you have, what theme are you using, etc. For example, my site loads faster when I remove the Facebook fan box from the sidebar – making that connection to Facebook slows things down.

  1. Kim Woodbridge says

    @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.

  2. Kim Woodbridge says

    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? ;-)


  3. Kim Woodbridge says

    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.


  4. Kim Woodbridge says

    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.

  5. says


    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

  6. Kim Woodbridge says


    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/

  7. says

    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!

  8. Kim Woodbridge says

    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 :-)

  9. says

    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 …

  10. says

    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

  11. says

    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!

    • says

      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.

  12. says

    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! =-.

    • says

      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?

    • says

      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


  13. says

    I am editing the code to the sidebar.php. here is the code:

    <a href=””>

    var countries=new ddtabcontent(“tabs”)

    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! =-.

  14. says

    Hm, let’s see if this works.

    <a href="">

    var countries=new ddtabcontent("tabs")

    .-= matthew´s last blog ..Camp Kingdom! =-.

  15. says

    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
    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 id=”tab2″
    ?php include (TEMPLATEPATH . ‘/simple_recent_comments.php’); ? ?php if (function_exists(‘src_simple_recent_comments’)) { src_simple_recent_comments(3, 100, ”, ”); } ?
    script type=”text/javascript”
    var countries=new ddtabcontent(“tabs”)
    .-= matthew´s last blog ..Camp Kingdom! =-.

  16. says

    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

    • says

      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(); ?>

  17. says

    Hi, if you want to use a “recent post function” for only one particular category here is a my example :

    // example here for the 7 most Recent posts for the category 5

    $output = '';
    $before = '';
    $after = '';
    while (have_posts()) : the_post();
    $post_title = stripslashes($post->post_title);
    $permalink = get_permalink($post->ID);
    $output .= $before .'' . htmlspecialchars($post_title). '';
    $output .= $after;
    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 =-.

  18. says


    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

  19. yesMan says

    Thanks for the code, it’s working great.

    However, I do have an issue with current-cat. The category in the code is always highlighted regardless of which category from wp_list_categories being displayed. Simple solution is of course not using the current-cat, but I want the active category to be highlighted, except the category in the code.

    I’ve tried to exclude the category in wp_list_categories but it didn’t do the trick. The category is gone but still no current-cat for other categories.

    Any pointers to this? Thanks!

  20. says

    Hi Kim
    This did exactly as you said, thanks! It is the “Current Discounts” listing of posts I didn’t want on my blog main page (chandelier-mall.com/blog).
    One thing I would like to add is to include a short (and not hyper-linked) content extract under each link. I s that something you know how to do?
    Thanks for this great resource!
    Best wishes
    .-= Julian´s last blog ..Lighting by Gregory Consistently Provide Great Value Discounts =-.

    • says

      Hi Julian – Below the area where you write a blog post is a section called Excerpt. If you add a sentence or two about the content of the post in that section, you can then call it in your sidebar code.

      Under <?php the_title(); ?> add
      <? php the_excerpt(); ?>

      that will add the little blurb that you added to the excerpt field.

      • says

        Hi Kim
        Sorry to be slow in showing my appreciation – all the volcanic ash has left my partner trapped in India so I’ve been pre-occupied somewhat! So – many thanks for your quick response.
        A couple of things:
        – I had to remove the space between the ? and “php the_excerpt” – then it worked.

        – What I was really wanting was for the extract text to NOT be (blue) hyperlinked, but to be in plain text.

        – In fact, to refine this even more, I would also like to NOT have the whole of the title hyperlinked.

        For instance, on the left sidebar of chandelier-mall.com/blog/ under Current Discounts, one of the links is presently “Lighting by Gregory Consistently Provide Great Value Discounts” which becomes 3 lines of hyperlinked blue text, which I don’t really like. What would be great would be to have, say, the first 3 words (Lighting by Gregory) as the hyperlinked anchor text and then the rest of the title (Consistently Provide Great Value Discounts ) makes a reasonable description by itself.

        Am I pushing my luck with these 2 requests?
        Actually, I’m starting to appreciate the php language more as I start to fiddle with it!
        Best wishes and thanks again!
        .-= Julian ´s last blog ..Eco-Alerting Blogs – Light for Home and Heart =-.

        • says

          Hi Julian – Wow! Trapped in India – hope everything works out ok.

          The code is going to pull the entire title – it doesn’t know how to break the title into the pieces that you want. But if you remove the permalink code surrounding it, it won’t be linked.

  21. says

    That works brilliantly Kim – many thanks! I can always adjust titles to keep them short, and tailor the excerpt to get what I want!

    Katja’s back from Delhi now. Aeroflot excelled themselves – 5 nights in 5 star hotels all paid for; then suddenly an announcement that they were flying out in a few hours – and it all ended quickly and very smoothly!
    .-= Julian´s last blog ..Celebrating The 2010 Milan Design Week =-.

  22. Sheridan Broderick says

    THANK YOU SO MUCH! I added the_excerpt() ; to collect the post excerpt and your code works marvelously! I tried a few other snippets but they wouldn’t work with the excerpt piece.

    Thank you again so much! I will definitely be subscribing to your blog!

    I have one question though. Each of the posts within this category are going to have a picture to go along with it. That picture is the first thing in the content of the actual post. Do you know how to get that thumbnail to show up in the excerpt?

    Thanks!!! =)

    • says

      Hi Danielle – Well you could link to a category page for that specific category or you could make a page template that only pulls posts from that category.

  23. says

    Thanks this has been very helpful.
    I’ve been trying to get recent posts from the category of a single post in single.php.
    Anyone happen to know how to do that?

  24. Ryan says

    Hi Kim – is there something I can do with the second loop to stop it from affecting the main loop?

    Here’s what’s happening —

    On index.php I have the main loop pulling the four latest posts. In my a sidebar widget I have your custom loop pulling the last post from one category. The idea is that this loop will feature pre-programmed posts that will publish on a daily basis.

    Unfortunately what’s happening is that with each day, and each new post displayed in the sidebar, a post from the main loop is pushed from index.php onto the archive page. The same thing happens with the archive pages — posts get bumped to a second page. After four days, and four daily posts, and no new posts in other categories, index.php features no posts!

    Is there a way to stop this from happening? Thank you for your fine work!

      • Ryan says

        Hi Kim, thanks for the quick response! I think what’s causing the issue is that I’ve asked for only four posts to display on index.php. I have a plugin (Clix Category Exclusion) keeping the daily, sidebar loop posts hidden from the main loop. So what’s happening is index.php will only display four posts, but the posts that are supposed to be hidden are bumping an older post from index.php and not replacing it. I suppose there is an work around for this, but it seems like a cause and effect type thing that may not be fixable!

        • Ryan says

          Hi Kim – you know what, I figured it out. I’ve switched plugins, and the Simply Exclude plugin seems to work much better — no issues with your loop idea. THANKS!

  25. says

    Kim this not working for me. I added Exec PHP and then added the code to a text widget in the sidebar and when I click on the category I get the same error.

    Here is the error message:

    “This webpage has a redirect loop.

    The webpage at http://www.discountgymequipment.org/abdominal-exercise-equipment/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.”

    Any help would be appreciated
    .-= Anthony´s last blog ..Why SocialADR Might Be The Best Social Bookmarking Tool Yet? =-.

    • says

      Hi Anthony – I’m not sure what is causing that. Something else is conflicting with the code – do you have any other redirects set up?

    • says

      Hi Anthony – Without being familiar with the site, I really don’t know. The code has been tested in widgets so it should work. Something else must be interfering with code.

  26. says

    Kim, so i trying to give this another shot.

    I have the Exec-PHP plugin activated. I am using a simply clean theme called WPSimpy and it is widgetized.

    I placed the code in a text widget and added it, saved it.

    Now I view my site, and when I click on any category listing, i get the same error message.

    If you can take a look at it for me and tell me what might be the problem, I would highly appreciate it.

    (I have no redirects set up for this domain)
    .-= Anthony´s last blog ..Bowflex PR1000 Home Gym =-.

    • says

      Hi Anthony – If you look at the bottom of my left sidebar there is a list of 5 recent posts in the WordPress category. I copied my code and pasted it into a widget – as you can see, it’s working properly.

      I went to your site but didn’t see a sidebar block with this code so I can’t tell what is happening. Maybe there is something in your .htaccess file or maybe you have a directory with the same names as the category – although that usually causes a 403 error.

  27. Adrian Mirea says

    Thanks, this was very helpfull for me.

    But there is a minor problem… At the end you should call wp_reset_query(); otherwise this script might affect (as in break) other queryes existing in the current page:

    <a href="”>

    <?php endwhile;

    I used this on the main page to display the first 10 posts in a certain category, and if i had not called wp_reset_query() then the rest of the page would break.

    Thank you and God Bless.

  28. says

    I tried the solution that you suggested to “blog tools” i.e. to use the plugin at http://wordpress.org/extend/plugins/related-posts-by-category/
    It really looks like a perfect solution but I can’t work out how to use it :(
    I installed the plugin but could not find an explantion on what code to put in the post in order to get the list of posts and their thumbnails to appear. I am so sorry to send this to you but I could find no way to ask the question on the site that you refered to. If you could explain this it would be much appreciated!!! Thanks so much for a great useful site!
    .-= Nicole´s last blog ..Special Requests for Reward Charts =-.

    • says

      Hi Nicole – The code example on the plugin page will work. You have to edit the single.php template file – this is in WordPress under Appearance > Editor > single.php If you aren’t comfortable editing template files, this might not be a good solution for you.

      • Nicole says

        Thank you so much for your reply. Does this plugin appear on every page. Do you know of another solution that will enable me to have this list appear on particular pages? I thought that this is possible with the plugin but I guess not.

        • says

          Hi Nicole – That plugin is designed to work on the single posts page so it will shop up with each individual post. It is based on categories so it won’t work on pages.

  29. Ethan says

    Hi Kim,

    Thanks for this excellent post.

    Quick question. What do I do if I want to use this on a page that does not have blog posts?

    I put this code in my header, and it transforms all of my WordPress pages into blog pages.

    Does that make sense?

  30. sid says

    Hi Kim
    I just used your code. Its cool. But there is a problem.
    If I use the mentioned code; tags go beyond control. I mean if you clicks on any of the tags they show all the posts in category .

    is there any fix.

    • says

      Hi Sid – Are you using this in the sidebar or the main template page? Is the code for the tags within the loop in this article. Do you have more than one loop on the page? If so, you will want to use


      as mentioned in the comments.

  31. Hannah says

    Hello and thank you!

    I’ve been looking for something like this for a while, glad you posted this.

    Just one question — how do I do it so instead of just showing the 5 recent posts from each category, it shows all the posts.

    This might be silly but I tried instead of putting showposts=5, I put showposts=all and it didn’t work.

    Help! Thanks again.

  32. Chip says

    I, like many others, found you via google and a couple years after the original post :)

    Works great! I do, however, have a request to modify it. I’m actually using this on a page that is pulling the most current post from a particular category. Is there a way to modify your code to remove the link for the post that is already lsted on the page.

    Hard to understand what i’m talking about without seeing it: http://www.nwtechanddesign.com/diy

  33. Lee says

    Hi Kim,
    Great code, and good on you for answering everyone’s questions as well as you have!

    Now I hope you can help me too…
    I have the code in my Sidebar and I would like to display the latest posts for whatever category the user happens to be in *right now*.

    Basically, is there a way of changing:


    Or even better,

    …as I have my Pages set up to display Posts whose Category share the same name as the Page.

    • Lee says

      Okay so I figured it out based on the original Loop I’m using to actually display posts.
      I changed the line to say:
      And it works fine now for whatever category I’m in.

  34. Pushpak says

    Can anyone please tell me how to display list of posts from just 2 categoriesonly.

    I m having 5 categories , in a page i want to display the posts from 2 categories i.e ‘heart’ and ‘cancer’ only.

    Any help will be greatfull.

    Thanks in advance


  35. says

    you made my day kim. i wasted lot of time to get it done but now i did it within 5 sec after copy/pasting your code.

    Thanks alot.

  36. says

    Nice Post, Nice Site.
    I, also found you via google.
    I have been working on this problem for 2 days, and still cannot get it to work.
    At the top ( or bottom) of the “Members” page, I want to display just the lastest post in the category “Meetings” (cat_ID=10), in order to display the date/time/infor for the Next Meeting.

    I am using the code you have above, [ query_posts(‘showposts=1&cat=10’); ], and many variations, including some that use array. I have also added [wp_reset_query();] But I cannot get it to work !

    I can get Either 1 post, the latest post, irrespective of category,
    or I get All posts in the meetings category.
    But it will not honor both !

    Has anything changed in wordpress since this article. I have run across several other comments at various websites that report similar troubles.

    Otherwise, any suggestions as to what might be causing the fail?
    Any direction to look into is soo greatly appreciated.

    I am using wordpress 3.1.3, php5
    I have tried this in 2 themes, that are about 2 years apart in publication date.

    thanks in spades to anyone who can help me along in this path !

  37. universal web says

    I am trying to use this code on a page. It shows the list of posts as expected, but also the posts content at the bottom of the page which I do not want. I am not sure why this is happening. any Ideas?

    Thank you for a great tip and your site is awesome.

  38. says

    First thanks for the info. I am trying to fetch content from two posts side by side manner. but it goes into looping and the two post contents keeps loading N number of times.

    How to stop looping? could you correct and give me the code. As of now on my website i have kept the left handside content (current issue), beside the current issue another post content will displayed (Icon of the month)


    /* left hand side content fetched from post under category “Current issue” */

    Current Issue

    <a href="” title=””>

    /* right hand side content fetched from post under category “icon of the month” */


  39. says

    Ok for some reason when I added this to my sidebar, the only posts that would show up on the main blog area were from the category selected in that script. Anyone had that issue?

  40. says

    Hi! I would like to tell about my extension, it is a shortcut to get this working. It still isn’t the easiest to use but, please give it a shot. I’m still developing it, adding new options, so please feel free to complain about it.


    srry for using your comment space to do this, is there any way that I can refer your website?



Comments are closed. Please contact me for specific questions or subscribe to my newsletter.