How to Get Posts in Category

in Wordpress, Wordpress Themes

Currently I need get wordpress post(s) in the specific category. Say I need to get 5 posts in “Wordpress” category from this blog then display that list at my home/main page of this blog. How to do that? How to show recent posts from specific category?. Could you?

We can get and show latest post or current post from specific category if we know category id, category name or category slug. Here are the samples to do that:
Sample code to get last 10 post from category id = 1

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

How to get category id or where do I get category ID’s?
To find a categories ID you can go to /wp-admin/categories.php and click on one of the category names. The url of the page you are on will show what the category id is. eg wp-admin/categories.php?action=edit&cat_ID=1 means the category is 1

Also you can use the category name or category slug instead category ID number, by use in_category(”) function to get post from specific category or to separate post by category name. We can use in_category( ‘Web Development’ ) to get posts under Web Development category. Here is short example to get post in specific category name:

<?php
$post = $wp_query->post;
if ( in_category( 'Web Development' )) {
	// Web development tips...
} elseif ( in_category( 'Wordpress' )) {
	// How to custom wordpress...
} else {
	// etc.
}
?>

in_category function usually used by wordpress theme developer to styling a category page in Wordpress.

How to prevent posts in certain categories from showing up on my blog’s main index page. Yeah, we also can excluding certain categories from your blog main page. Here is sample code to exclude category id 1 and 2 from home/main page:

<?php if ( is_home() ) {
query_posts($query_string . '&cat=-1,-2');
} ?>

Here they are some useful references
Function Reference – in_category()
This tag can be used to test the current post within The Loop or (since Version 2.7) outside the Loop during a single post request. You can use it anywhere if you specify which post you want to test.

How to Set Up Custom Wordpress Category Templates in Four Easy Steps
Step by step guide to set up custom wordpress templates

Use the social buttons below to add it on your favorite social sites to get the latest updates. Not sure about these options? Learn more about RSS and social bookmarking

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

{ 2 comments… read them below or add one }

1 Emma Smith February 13, 2010 at 3:35 am

This is a nice tip. I'll have to try this out on my blog and see if the visitors like it, seeing recent posts from each category.

Reply

2 Sheraton Hotel March 11, 2010 at 5:42 am

publish the post on specific category is a big problem it takes lots of time but you make it easy thanks

Reply

Leave a Comment

Previous post:

Next post: