Dec
02

Retrieving Posts from a Particular Category

Hacking WordPress

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

Now just below this line, add the following piece of code:-

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

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

This post was viewed 15,873 times

24 comments on “Retrieving Posts from a Particular Category”

  1. Telecharger says:

    Thanks, great timing as I need this one now.

  2. Many people have asked me how to show posts on their homepage only from a particular

  3. Not there either. I found what I was looking for at http://DoWeSync.com . They sync People not Data..

  4. Anne Armel says:

    I have learn some excellent stuff here. Definitely worth bookmarking for revisiting. I wonder how a lot attempt you put to create this type of wonderful informative web site.

  5. my blog says:

    Not long ago i found your blog and have been looking at together. I have seen several unusual feedback, nevertheless generally I’ve got to agree with what are the additional commenters are generally writing. With the amount of greatgreat testimonials of this weblog, I figured that i also need to start along with advise you that I enjoyed scanning this your articles. Therefore i think this would help to make my 1st opinion: “I consider you have produced a few truly intriguing items. Very few men and women would actually think about it the way you only did. Now i’m actually impressed that there are a great deal about this topic that have been discovered and also you made it happen consequently well, with a great deal class!inch

  6. movovi says:

    Very helpful. Thanks !

  7. Boloindia says:

    I am not a coder but it sure helped me in a way. Now i excluded some category posts on my website and it looks stunning. :) Thanks by the way i liked the BIG NEWS theme. Don’t have money to buy at the moment surely will add it to my collection soon… chao

  8. ymm0t says:

    Thanks! Will bookmark this.. :)

  9. atoranga says:

    thank you, you solved my problem

  10. Irfan says:

    Cool!
    I like category more than tag.

  11. Molli says:

    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.

  12. Raol says:

    Thanks.. i was really in need of it…!

  13. fedmich says:

    Thanks, great timing as I need this one now.

  14. Sarah says:

    Great tutorial! I have been looking for this from many days. Thanks for sharing.

  15. }Ja{???? says:

    oh~thankyou, i found you, is so useful

  16. moDesignz says:

    hi…I need to be able to display a sort of “featured design” section on the front page of my website.

    I was thinking maybe I put a code that shows the latest post which is taking out the codes for the title, meta data and content out of the loop and putting them alone to show only one post which is the latest. I want to do this for a particular category and add a post to that category everytime I want to suggest a featured design.

    But I also want to hide it. I mean hide the category with the “featured designs” so that it doesn’t appear in the nav

  17. SeanCHEN says:

    Thank you very much!!! It helps me a lot!

  18. Emir says:

    Hi, I was wondering is it possible to show some unique content on specific post? Let’s say I have only 5 posts where I want to add some content but not anywhere else.. Possible??
    Thanks!

  19. Dhan says:

    Very helpful tip. Thanks

  20. doniek says:

    Very good tutorial, thx :)

  21. Nice tutorial you got there! thanks for sharing

  22. 365Hope.com says:

    It is interesting. Thank you! Jai. I will try it on my blog (www.365hope.com).

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">

Search :