Posted on 1 October, 2008 By Kim Woodbridge

WordPress - How to List Recent Posts from One Category

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

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Ma.gnolia
  • Reddit
  • TwitThis
  • E-mail this story to a friend!
  • StumbleUpon
  • Technorati

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
Posted In : Howto | WordPress


If you enjoyed this post, please subscribe to my RSS Feed



Comments

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

Yip, works like a treat. Thanks for another good tip.

Dave’s last blog post - Choosing the right ecommerce platform for your microbusiness

Kim Woodbridge
Gravatar

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.

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

Kim Woodbridge
Gravatar

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!

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!

Kim Woodbridge
Gravatar

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!

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

Kim Woodbridge
Gravatar

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.

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

Kim Woodbridge
Gravatar

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/

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!

Kim Woodbridge
Gravatar

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

RSS feed for comments on this post. | TrackBack URI

Leave a comment



Recent Comments

    • Raju: damn good... thank u so much!! Raju´s last blog post - ...
    • Carla: My blog is relatively new and so far Google works pretty well for the time being. I never searched for my own blog thou...
    • Kim Woodbridge: @Cath @ Carla I located this information about the problem you are having with visual text editor. Maybe it will hel...
    • Kim Woodbridge: @Raju - Thank you for visiting and commenting. I think there are plugins that already do that. You could take a look...
    • Kim Woodbridge: @Matt - Thanks! I really appreciate it. Maybe I'll try google search this weekend....
    • Matt Gio: @Kim - So far google has been doing pretty well for me. But if I come into any problems I'll let you know. BTW - I adde...
    • Raju: Hi, i would like to have a plugin which can give me a list of available tags related to the post I am going to publish,...
    • Kim Woodbridge: @Stratos - That was another thought I had - how many people even use the WordPress search function. I use it occasional...