Display Subcategories on Category Pages in WordPress
When using WordPress as a CMS, or even as a blog you might want to display subcategories on category pages. There is really no easy way to do this and some ways that are out there on the web has various bugs. In this article we will show you how you can display subcategories on category pages using this hack.
Now the hack shared on Yoast’s blog is outdated and has bugs. The bug is that even though it shows the subcategories on the category page, but when you go inside the subcategory, it does not show any category. With the hack we are sharing it will show subcategories on the parent category pages, and when you click on the subcategories, you will still see other sibling categories in the parent category.
Ok if the above terminology did not make sense to you let us explain in another way. Lets say you have a top level (parent) category called Sports. You have three subcategories under sports called NFL, NBA, and MLB. Now when someone arrive to your homepage, you will only see Sports category and other top level parent categories. When someone clicks on Sports, the categories will now show only the subcategories under Sports, so it will be NFL, NBA, and MLB. Now with Yoast’s hack if you click on NFL, everything will be gone. With our hack when you click on NFL, it will still show NFL, NBA, and MLB.
So basically this hack is allowing you to List subcategories if viewing a Category, and brothers / sibling categories if in subcategory.
<?php
if (is_category()) {
$this_category = get_category($cat);
}
?>
<?php
if($this_category->category_parent)
$this_category = wp_list_categories('orderby=id&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
"&echo=0"); else
$this_category = wp_list_categories('orderby=id&depth=1&show_count=0
&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
"&echo=0");
if ($this_category) { ?>
Paste the above code wherever you like the categories to display and modify it however you please. The code listed above, when you visit the subcategory from the main category, hides the main category and only shows the subcategories.
If you want to show the main category also on subcategory pages, just remove the parameter depth=”1″ from the code above.
This is a very useful and handy category hack when it comes to designing templates for WordPress specially when using WordPress as a CMS.
For further details on parameters refer to WordPress Codex.
Comments
15 Responses to “Display Subcategories on Category Pages in WordPress”Share Your Opinions
Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!
Please make sure that you have read our Comment Policy.











Excellent post, thanks for the snippet.
Hi,
I’m trying to accomplish this in the sidebar, only instead of links, I want to list recent posts in subcategories.
and of course, this would occur in all category and subcategory pages, as well as single posts.
Don’t know if this is possible. But, I’ve been looking around like crazy, and not being a programmer isn’t helping either.
Here try this post and use Hack #4 (Display Recent Posts from Specific Categories)
Thanks for posting this. I got my posts displaying fine, but had forgotten about the sub cats…it makes more sense this way.
Hi, thanks so much for this code and I have it working on my template. How would I prevent this entire code from running if a category has NO children? If there are no children, it creates an empty list and prints the text “No Categories.” Here’s how I had it setup with Yoast’s code:
cat_ID) != “”) { ?>
Subcategories:
cat_ID);?>
If there were no subcategories, nothing would show up. Your code is better, but I need it to only show up ONLY if there are subcategories. How would I achieve this? Thanks for any suggestions.
I’m getting a “Parse error: syntax error, unexpected ‘=’” error from this?
Ideas?
Try it now
It should work. The code just converted the quotes into curly quotes.
This is a great starting point for me as I had been searching high and low for this.
I wondered if there was a way to elaborate on this and allow say, 1 post title and excerpt then 4 more titles from each subcategory, displayed in a div.
I have been trying to find a way to do this for months but have never found anyone able to assist. I even offered a small payment but no-one came forward
Thanks if anyone can offer any advice
Karl
We can help you if you contact via email. There is a way to do it that can be explained either via email, or we can do it for you for a small payment
Hi,
This code is exactly what I need, only I want to use it in the actual category.php content section, not in the sidebar. Anyway, I pasted it in and I get a parse error. Is there something missing at the end of the code snippet?
Thanks.
-Rachel
So many things can matter. Where exactly did you place it… please contact us via our contact form for help.
Rachel, did you get this figured out? I’m getting the same error.
I’ve heard that this code no longer works since the WP upgrade. I also got a parse error.
Where did you hear this?
testing with v 3.0 doesnot work.. buggy