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,834 times

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

  1. oh~thankyou, i found you, is so useful

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

    Reply to this comment
  3. Thank you very much!!! It helps me a lot!

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

    Reply to this comment
  5. Very helpful tip. Thanks

    Reply to this comment
  6. Very good tutorial, thx :)

    Reply to this comment
  7. Nice tutorial you got there! thanks for sharing

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

    Reply to this comment
« 2 1

    1 Trackback(s)

  1. Dec 7, 2007: Adding Extra Sidebar to your Wordpress Theme « goe on the cyberspace

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