Most WordPress themes, by default, come with Recent Posts displayed automatically. Depending on the type of blog you run, it is possible that you would prefer to display Recent Posts per category. If this is the case, here is what you need to do to only display recent posts for specific categories.

First, you’ll want to find your Recent Posts code, which is usually found in the sidebar. It will look something like this:

<h2>Recent Posts</h2>
<ul>
<?php get_archives('postbypost', 10); ?>
</ul>

As always, make sure you have a backup of the file in question before making any changes. You’ll then want to replace the above code with the following code:

<ul>
<?php $recent = new WP_Query("cat=1&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>

Where is says cat=1, you’ll want to insert the number assigned to the category that you want to display the posts from. You can find this from your Manage -> Categories page. You can also adjust the number of posts to be displayed where it says showposts=10.

This could be useful to someone who wants to display the most recent few posts from a few different categories in their sidebar.

[via ReadyWPThemes]

Kyle Eslick is WordPress enthusiast who took his passion for WordPress to the next level in 2007 by launching WPHacks.com as a place to share hacks, tutorials, etc. Connect with Kyle on Twitter or Google+!

  • http://www.blogtuga.org Alexandre Pereira

    Thanks for the tip.

  • Pingback: How To: Display the Recent Posts of Specific Categories « PHP Net

  • http://www.blogtuga.org Alexandre Pereira

    One more question. How to show posts of various categories excluding the one we have put before, using the code above?

  • Pingback: Compilation of WordPress Hacks

  • http://www.101waystomakemoney.com Brock Hamilton

    Exactly what I was looking for thanks. its amazing how the wordpress support forum just tells you to go figure it out for yourself,  but I can come here and get the answer. you guys rock.

  • http://www.cfdspy.com Phil

    Hi, thanks, works a charm, I wondered how you would go about adding (and controlling the length of) a few lines of the article text to this.

  • Pingback: 100+ Killer Wordpress Resources | Steffan Antonas' Blog

  • http://n7182y.com John McCue

    Thanks for this tip. I’ve been trying to figure this out for a couple of months. Does this only work in the sidebar?

  • http://themonkeynuts.com/root/?p=157 James Marr

    Great!!!
    Worked first time, tried loads of other tutorial but this was the best by far.
    Thanks for your help ;)

  • Sciabb

    I can’t get it to work, if after cat I place the name of the category it still shows me all posts. Instead of placing the name I tried to place a numeric value… but the result is that it shows me nothing if I place 0 and 4, it shows me all posts if I type 1,2 or 3. Can anybody help? thanks

  • http://www.jessicahorton.com Jessica Horton

    I’ve tried like 5 or 6 different tutorials and none of them worked! — Yours did!

    My only question is how can I add a line break between the titles? They are stacked right under each other and a little extra space would be nice! Look, I sell real estate and only dabble at this stuff — forgive my lameness! :)

  • http://www.kapadokya.net kapadokya

    Exactly what I was looking for thanks. its amazing how the wordpress support forum just tells you to go figure it out for yourself, but I can come here and get the answer. you guys rock.

  • Rajeev

    Thanks very much. I am an absolute newbie and was working on a readymade template. my client gave me this requirement and you gave me solution. thanks a zillion.

  • Pingback: 33 Specific to WordPress Category Hacks And Useful Resources

  • sagive

    10x man.. needed one with exceprt but this is a good start :)

  • http://www.mensbestgifts.com/ Camden

    Do you know if this would work on a main page rather than the sidebar? I tried it…looked ok on preview but I’m nervous that I’ll break my site. Also is there a way to display an excerpt rather than just the title?

    Thanks for your help!

  • http://www.octographicdesigns.com Andy Sochor

    Just what I was looking for. Thanks!

  • Jelbee

    Can this be applicable in any templates?

  • http://www.towingphoenix.info Ana

    Thanks for this tip. I’ve been trying to figure this out for a couple of months. I’ve tried like 3 different tutorials and none of them worked! Expect yours.. thanks.

  • Theresa82

    This post tip about “Recent Post” is exceptional. I tried it and it works well with mine. Thanks for sharing this code.

    Dreamweaver CS5 tutorial

  • http://www.webkia.com webkia

    Will it also display the posts from subcategory of the category mentioned in this list.