PreviousPauseNext
Latest Free WordPress Themes | More Free Themes
Portfolio WordPress Theme
Portfolio Press

Dark Colored
Valid CSS/XHTML
Widgetized
Unique Layout
Styled Comments
Neat Code

Demo
Download
Latest Free WordPress Themes | More Free Themes
DailyPress WordPress Theme
DailyPress

Minimalistic Theme
125x125 Ad Space
Widgets Ready
Social Tagging
Tabbed Sidebar
XHTML Compliant

Demo
Download
Latest Free WordPress Themes | More Free Themes
Firebug WordPress Theme
Firebug

Colorful Theme
125x125 Ad Space
Widgets Ready
Social Tagging
Gravatar Ready
XHTML Compliant

Demo
Download
Latest Free WordPress Themes | More Free Themes
Gridblog WordPress Theme
Gridblog

Minimalistic Design
WP Gallery Ready
Gravatar Ready
Grid Based
Widget Ready
XHTML Compliant

Demo
Download
Latest Free WordPress Themes | More Free Themes
Blue Weed WordPress Theme
Blue Weed

WP 2.5 Gallery Compatible
Adsense Ready
Gravatar Ready
3-Columns
Widget Ready
Logo PSD

Demo
Download
Latest Free WordPress Themes | More Free Themes
AdsPress WordPress Theme
AdsPress

WP 2.5 Gallery Compatible
Adsense Ready
Gravatar Ready
2-Columns
Widget-Ready
Logo PSD

Demo
Download
Latest Free WordPress Themes | More Free Themes
Statement WordPress Theme
Statement

WP 2.5 Gallery Compatible
Quick Tabs
100% Width
3-Columns
Widget-Ready
Logo PSD

Demo
Download
Latest Free WordPress Themes | More Free Themes
Tutorials WordPress Theme
Tutorial Theme

Tutorials Feature
Thumbnails
Bright colors
3-Columns
Widget-Ready
XHTML Compliant

Demo
Download
Latest Free WordPress Themes | More Free Themes
Corptheme WordPress Theme
Corptheme

Four Flavors
Dropdown Menu
Separate Trackbacks
3-Columns
Widget-Ready
XHTML Compliant

Demo
Download
Latest Free WordPress Themes | More Free Themes
Chronicles WordPress Theme
Chronicles

Dark Brown
3-Columns
Widget-Ready
XHTML Compliant
Web 2.0

Demo
Download
Latest Free WordPress Themes | More Free Themes
Velocity WordPress Theme
Velocity

Trackbacks & Comments
3-Columns
Widget Ready
XHTML Compliant
Web 2.0

Demo
Download
Latest Free WordPress Themes | More Free Themes
Blue Lily Theme
Blue Lily

3-Column
Widget Ready
XHTML Compliant
Web 2.0
Rounded Edges

Demo
Download
Latest Free WordPress Themes | More Free Themes
Techicon Theme
Techicon

4-Column
Widget Ready
XHTML Compliant
Blue/White
Recent Boxes

Demo
Download
Latest Free WordPress Themes | More Free Themes
Brilliance Theme
Brilliance

3-Column
Widget Ready
XHTML Compliant
Black/White
Adsense Ready

Demo
Download
Latest Free WordPress Themes | More Free Themes
Identity Theme
Identity

3-Column
Widget Ready
XHTML Compliant
Black/White
Rounded Edges

Demo
Download
Latest Free WordPress Themes | More Free Themes
Xplosive Reloaded Theme
Xplosive Reloaded

3-Column
Widget Ready
XHTML Compliant
Multi-colored
CSS Style Switcher

Demo
Download
Latest Free WordPress Themes | More Free Themes
Limit Theme
Limit

3-Column
Widget Ready
XHTML Compliant
Blue/White
Rounded Edges

Demo
Download

Retrieving Posts from a Particular Category

Written by Jai on December 2, 2007 – 5:38 pm - 4,060 views

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.

If you enjoyed this post, make sure you subscribe to my RSS feed!


14 Responses to “Retrieving Posts from a Particular Category”

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


    Reply to this comment

  2. Nice tutorial you got there! thanks for sharing


    Reply to this comment

  3. Very good tutorial, thx :)


    Reply to this comment

  4. Very helpful tip. Thanks


    Reply to this comment

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

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


    Reply to this comment

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

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


    Reply to this comment

  9. thanks.. i’ll put in on http://www.downloadbokep.cn


    Reply to this comment

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


    Reply to this comment

  11. Thanks, great timing as I need this one now.


    Reply to this comment

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


    Reply to this comment

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

  1. 1 Trackback(s)

  2. Adding Extra Sidebar to your Wordpress Theme « goe on the cyberspace

Post a Comment