How to Display Recent Posts From A Specific Category In WordPress

Posted on July 10th, 2009 by in Tutorials | 7 Comments  
How to Display Recent Posts From A Specific Category In WordPress

For advance users who want to display recent posts from a specific category, we have this article specially for you. Beginner users will most likely not use this. This tutorial is mostly used in creating magazine style themes.

<?php
query_posts('posts_per_page=1&cat=3');
while(have_posts()) : the_post();
?>
<ul>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>

<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>

At the top where it says showpost= change the number to how many posts you want to display, and cat=3 is the id of the category, so change the ID of the category to pick which category will you be displaying.

A live example of this tutorial in action is at Balkhis.com

The news style format where it displays recent news from each category is accomplished using the code above.

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest
1joshmay 5 pts

How could I include a thumbnail of each post to this?

RichardGauthier 5 pts

Is there a way to put that code or whatever something similar, in the sidebar?

ibadullah25 5 pts

How this code works?

I mean where should I paste this?

wpbeginner 50 pts

ibadullah25 Paste the code in the file where you want to display this... Usually a code like this is used in magazine style themes often on the homepage.

ibadullah25 5 pts

wpbeginner Yes, I am using a magazine style theme I paste it in the single.php I want to display in posts not on the index page and the code is not have closing tags?

Hey,
the site mentioned above was built as part of experimental project. it was built from scratch with no modifications what so ever.

Lately, after discovering you helpful site (you guys are doing well here), i used the code mentioned here and then funny thing happened; it took over the title (the title changed to 'Reviews' - reviews is the category im pulling) . the title before that was fine and when I remove the code the title is going back to normal... i should mention that i'm using also All in One SEO...
any thoughts??

thanks you for your time!

Tweets about us: