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

By Matt Dunlap on September 22nd, 2009
Tags: ,

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:   
alert icon Do you need help with your website? I can offer full system admin, website development and Internet marketing services
Domain Name Deals
.COM for $1.99
With any non domain name purchase
Free Private Registration - When you buy or transfer 5 domains at opendls.com
Domain Name Reseller Accounts
Low buy rates - Very Profitable
Matt Dunlap on Why am I getting redirect loop errors with FireFox 3.6.8?
Hi, thanks for the heads up... Have you experienced this problem with any other websites? It's reall[...]
Stacy on Why am I getting redirect loop errors with FireFox 3.6.8?
I was attempting to follow your links to these tutorials from your youtube video, and I was using th[...]
Matt Dunlap on 301 redirects will lower your Google Pagerank
You want 301 redirect on same site redirects mainly to eliminate duplicate content. For example, if [...]
Kate Mag on 301 redirects will lower your Google Pagerank
great info. Does this mean that only affecting 301 redirect to another domain. I have 301 redirect t[...]
Mike Johnson - Real Estate Agent on Why is making extra money a bad habit?
Hey man, this is some great stuff. The only thing I can't figure out is why this sort of thing isn'[...]

Top Guest Bloggers

LoneWolf
View Posts | RSS

Guest Bloggers Welcome!


Powered By: Free Premium WordPress Theme | Copyright 2007 - 2010 mattdunlap.org Sitemap