Retrieving Posts from a Particular Category

Many people have asked me how to show posts on their homepage only from a particular category or categories. So here is a simple and quick solution for doing this.
Getting Posts from a Single Category
Go and open your index.php file from your theme folder. Now search for this string :-
<?php if (have_posts()) : ?>
Now just below this line, add the following piece of code:-
<?php
if (is_home()) {
query_posts('category_name=General');
}
?>
The above code will display posts only from the category called “General” on your home page. You can change this to any of your category names.
Getting Posts from more than one Category
In another case, if you want to show posts from more than one category (for example “General” and “Graphics”), you will have to use the category IDs and the code will be written like this:-
<?php
if (is_home()) {
query_posts('cat=1,2');
}
?>
In the above example, I have assumed that the category ID for “General” is 1 and for “Graphics” its 2. You can get the ID for your categories by going to Manage->Categories in your wp-admin.
Excluding Posts from certain Categories
And if you want Wordpress the exclude posts from some category, just put the “-” (minus) sign in front of the category ID like this:-
<?php
if (is_home()) {
query_posts('cat=-1,-2');
}
?>
The above code will show all the posts on your home page except from the categories with ID 1 and 2.
I hope this is helpful for everyone.



































Thanks! Will bookmark this..
thank you, you solved my problem
Cool!
I like category more than tag.
Thank you so much for this. I’ll be bookmarking it for future reference for a band’s website I’m putting together. This little tip will come in extrememly handy, I think. Thanks again for posting it.
Thanks.. i was really in need of it…!
Thanks, great timing as I need this one now.
Great tutorial! I have been looking for this from many days. Thanks for sharing.