colorpress - Premium WordPress Theme
$49.99

ColorPress is a theme that lets you play with color! Now, you will not be restricted to using a few color schemes provided by the developer. You have all the colors to apply to your theme! Just pick your color and magic happens. The theme comes with a configurable slideshow, 3 blog layouts, dropdown menus and a load of other options that will rock your socks!

Wix.com

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

This post was viewed 7,856 times

» 17 people left comments { Collapse all | Expand all }

  1. Thanks! Will bookmark this.. :)

    Reply to this comment
  2. thank you, you solved my problem

    Reply to this comment
  3. Cool!
    I like category more than tag.

    Reply to this comment
  4. 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.

    Reply to this comment
  5. Thanks.. i was really in need of it…!

    Reply to this comment
  6. Thanks, great timing as I need this one now.

    Reply to this comment
  7. Great tutorial! I have been looking for this from many days. Thanks for sharing.

    Reply to this comment
2 1 »

» Leave a Comment!

Search :
Browse :
Archive :

$$$ Make Money $$$

Become an Affiliate

Welcome to Blog Oh! Blog's affiliate program! I personally invite you to make money by selling all of Blog Oh! Blog's Premium themes.

You will get a whopping 25% share on each sale generated through your affiliate link.

All you need to do is sign up as Blog Oh! Blog's affiliate, choose a banner, paste it on your website or blog and wait for sales to happen. It's as simple as that!

All affiliates will be paid on 10th of every month. Your affiliate earning needs to be a minimum $50 to get paid for each month.

Here is the sign up link and here are some banners to get you started!