How to Make a WordPress Category Page Without Showing the Category Children

By Matt Dunlap on September 22nd, 2009
Don't forget to tweet this post for a chance to win a $100 BestBuy Gift Card
Time Left: | Details and Rules

wp_iconI recently ran into a problem with the way WordPress handles categories and subcategories. My wife wanted to add a new section to her real estate website. Her brokerage gave her a large collection of real estate buying guides. The new articles all belonged to categories already, and she already had a section for buying guides.

I decided to make subcategories in her current buying guide section and add the content to the subcategories. I would then make a custom category page using the wordpress category template “category-<cat_id>.php”.

WordPress uses templates to manage many of the common features on a blog. Categories is a special template that uses a hierarchy to display the category pages. The hierarchy is a follows: category-<cat_id> will override category.php which will override archive.php which will override index.php.

Therefore, I can have a default category page for all the other categories, except for this special category for buying guides.
Read more about category templates here

On the custom category page, I wanted to list the 5 most recent blog posts under the main category “buying guides” and then list all the subcategories and the blog posts in the subcategories.

The problem I ran into was that all children are displayed under a category when using the wordpress function get_posts(‘cat_id=x’).

The solution is to use the wordpress function query_posts(array(‘category__in’=>array(x))) – NOTE THAT IS A DOUBLE UNDERSCORE

query_posts will also take the argument for cat_id, or category name, but again, it displays all subcategory posts.

//$cat_id = main category id
query_posts(array('category__in' => array($cat_id)));
while (have_posts()) : the_post();
//do something with the main category
endwhile;

Then to get the children, I can do the same with each subcategory

$categories = get_categories('child_of='.$cat_id);
foreach($categories as $c)
{
query_posts(array('category__in' => array($c->term_id)));
while (have_posts()) : the_post();
//do something with the subcategories
endwhile;
{

My next post is going to be awesome... Subscribe to my feed so you don't miss it

Large RSS Feed Button


Previous Post Next Post

Post a Comment

Name:   
Email:   
Website:   
Comments:   
The Money Coach on Brian Timpone Responded to my Blockshopper.com Complaint Blog Post.
I respect that there is an open debate on this issue. The great thing about the internet is we have[...]
Thom Living Green on How to enable WordPress 3.0 to run multiple websites
I followed your instructions and it worked just like you said. I now have a secure wp 3.0 mu set up [...]
Brocha Weiss on Blog Math - Numbers don't lie when it comes to success.
Hi Matt, I agree when you said, if there's no traffic to your site there is no money. This is the[...]
RoC Skin Care Fan on Your most effective keywords are not in your blog post
Spot on advice about targeted keyword anchor text. Its amazing how many websites do not realise thi[...]
ACME Affiliates - Affiliate Networks on Take control of your blog advertising with OpenX Adserver
OpenX is a great solution for managing ads on any kind of site, not just a blog. The problem that mo[...]
Matt Dunlap on Jquery Anything Slider Plugin for WordPress Magazine Themes
That is so cool that you modified it and made it better... I'll fix my code, and make the changes W[...]
Powered By: Free Premium WordPress Theme | Copyright 2007 - 2010 mattdunlap.org Sitemap