Wordpress Recent Posts from specific category
By hakan
As you know we use :
<h2>Recent Posts</h2>
<ul>
<?php get_archives('postbypost', 6); ?>
</ul>
to show our recent posts. But how do we show recent posts from specific category ?
To do this we use :
<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>
here cat=1 is our category ID number and showposts=10 is our recent posts from specific category count. When you add this code to your sidebar.php file, you have to check your specific category ID from you admin panel, manage, categories.
I hope it is work for you …
Topics: Worpress Tricks |


Comments
…nice tip. I will implement this and see how it goes… thnx
Hey Friend thanks a lot for this mice tip, actually it’s very useful for a guy like me with not much knowledge of PHP stuff. So it really helped me a lot and indeed much better option than going for a separate plug-in installation.
Keep the good work!
Thanks for your interest…
Can i show Recent Posts more than one category?
Yes sure, just duplicate same loop with different category ID.
Thank you! Exactly what I was looking for. Wanted to only list Recent Articles. So I used your code in a ‘php execute’ widget. Worked perfectly
Great! Thank you!
Is there anyway to include post images with this?
Thanks a lot!
To list from more than one post you could do this:
have_posts()) : $recent->the_post();?>
<a href=”" rel=”bookmark”>
works like a charm. Thanks for the great update here!
Sorry, wouldn’t display all the code so here is the line: WP_Query(”cat=5,3,7,6,4&showposts=10″);
works perfectly thanks
Hi,
When Upload some pages like About or Contact, even that appears in the list.
Is it possible to exclude few specified post so that they don’t appear?
Thanks,
Rajveer
Hi Rajveer,
I don’t have any idea about your question. As you see this method available to list all posts from specific category. You may check wordpress codex for your question.
thanks!
this is wonderful, thank you so much..
Do u by any chance know how to display all recent posts EXCEPT from a specific category?
Thanks in advance
@Shira,
You can use Recent Posts widget(From WP Admin Panel =>presentation=>Widgets) for this.
Shira was asking to display all posts EXCEPT one category. Is there any way of doing this? I’m looking for the exact same thing! Thanks!
@Steve,
Did you read my answer and try with Recent Post widget?
This is just for your themes or can it work for any wordpress theme?
yes, sure Joe…
how about open permalink in a new window? _blank :S
ah, got it. The error was some dumb letters
<a href=”" target=”_blank” rel=”bookmark”>
Note the target=”_blank” added.
Trackbacks