Showing subcategories on WordPress category pages

I wanted to show the subcategories for a given category on it's category page in the sidebar, but somehow the code for this wasn't readily available. Basically you need to check whether there are any children, and if there are, list the categories with the current category as a parent or grandparent. The following code does just that:

if (is_category()) {
  $this_category = get_category($cat);
  if (get_category_children($this_category->cat_ID) != "") {
    echo "<h1>Subcategories</h1>";
    echo "<ul>";
    wp_list_categories('orderby=id&show_count=0&title_li=
&use_desc_for_title=1&child_of='.$this_category->cat_ID);
    echo "</ul>";
  }
}

Have fun with it!

Tags:
Category: WordPress
You can skip to the end and leave a response.

74 Responses to “Showing subcategories on WordPress category pages

Cool thanks joost, i was searching for something like this.

Cool, don't hesitate to shout out other things you'd like covered regarding WordPress!

Thanks for the hack / mod, dude. I consider the WordPress category system to be their weakest link and this little hack should help out quite a bit.

Thanks

My pleasure :)

I'm having problems with displaying subcategories. When I create a sub category it shows on blog pages at its a main category, i.e. it is aligned separately in alphabetical order. I added your code to sidebar.php , but it changed nothing. Please help.
btw, When I switch back to default wordpress template it does show sub cats under its main cat.

I figured it out myself. There was a problem with current template, by replacing some code lines with default theme sidebar, it worked. Thanks :)

Hey TechDuke, nice to know you figured it out :) Was anything wrong in my code?

I just checked your code again on my blog and its working great, maybe previously I didn't notice the change, as I only had 1 subcategory.

Good, nice to know :) thx!

Sorry if you feel it off topic, but I really need to know. How do I link to posts pages in numbers like 1,2,3 instead of "next entries" or "previous entries" on main page and category pages.
I like the way you are linking to your previous entries pages. Is there any plugin I can use?
Thanks You.

TechDuke

Thank you VERY much. Let me test it :)

Cool :)

Tried this, unfortunately no categories show at all. Any ideas?

lapsaJ: does it have subcategories? :)

Yeah. I have one category which has a category inside that, and another category inside that.

Hi Joost,

Help me please! :D

Viewing a category page. If it haves subcategories, show the subcategories list like link-post-names at top of the page.

Can u please help?

Many thanks. Keep up the great work. =)

hi n37, mail me at joost at joostdevalk dot nl and I'll see what I can do to help :)

Thanks Joost for all your help! :)

I use the code for creating a submenu in the sidebar of an ordinary website, not a blog. It works very nicely – but there is one nasty little problem.
Everything ist fine, when I'm looking at the main-category page. Submenu is there, showing the subcategories.
But clicking at the submenu-link and proceeding to the subcategory-page makes the menu disappear. That ist: The submenu doesn't show up on subcategory-pages.

Can you please give me a hint?

Hi,

Can you explain exactly where to palce this code,
I copied into sidebar, and the subcategory showed but way down the bottom of the menu, how would I make it display directly under the top category.

@rich, try with some CSS / DHTML code instead =)

Hi,

Can you please tell me where to insert that code???
I dont know PHP... but can follow thing if you can give me some directions. pls.

Thank you.
Regards
Raj

Hi Raj,

wherever you want those subcategories to appear actually... There's no "set" place for them. It will probably be in sidebar.php though if you're using a non widgetized wordpress theme.

Perhaps you could also adjust your code along the lines of the below, as it keeps the list when visiting those subcategories.

cat_ID == 21) or ($this_category->cat_ID == 35) or ($this_category->cat_ID == 23) or ($this_category->cat_ID == 34)) { ?>
cat_name; ?>

category_parent == 21) or ($this_category->category_parent == 35) or ($this_category->category_parent == 23) or ($this_category->category_parent == 34)) { ?>
category_parent, FALSE, '', FALSE)); ?>

category_parent.'&Categories=0&hierarchical=1'); ?>

Cheers!
Iain

Is there a way to show this list with the subcats description for each one? (Like a post archive, but instead of the post content, the category description)...I'm breaking my head trying to figure out that...

I have the exact same problem as Kirsten!

Ray & Kirsten - see my post above using category_parent.

Thanks for the code,
Can you show the full code. I mean those with the "".

I do not know where to put those.

Please advise. Thanks.

Thanks for the code..
but i don't know which file i should put the code in...

Help please. thanks

Thank you so much for this code - I got the link here from the WP forums.

Is there a way to show ONLY the current-cat's children (& not great-grandkids)? Have you worked with collapsing/ folding categories in wp?

And, any tips why your code would insert a blank ul marker before the list? (i've checked, and I don't have any weird category names-ok, maybe they are weird, but ya-know-what-I-mean?)

Thanks a ton again for sharing this snippet!
Cathy

When I click on category in top nave. It shows me the subcategories in sidebar. I was looking for the same it works very nicely but there is one little problem.

But when I click any article title. The sub categories disappears :(

Can you please give me a hint?

Great ! Thanks for the Share :)

can anyone show me the demo?
thanks

Hi joost,

I have a question. I want to create a wordpress blog that has subcategories pages which appear only under certain other pages.

For example, I would like the page "cat" to appear only in the sidebar under the page "animal"

I am talking about pages and not posts

Please help
ben

Dear Joost de Valk
I was overjoyed when I found this page as it is the answer to exactly what I want to achieve ! Unfortunately I am not a code person. I am not lazy, but code is not my expertise ! I was wondering, to enhance this page if you could give a more detailed instruction into how to initialize this code in a blog.
If I paste it in its current form into sidebar.php it just gets printed out onto the html page 'as is', without creating any functionality.
Is there a specific place it needs to go ? Does it need wrapping in any other tags ?
Any help would be really appreciated...

How do I keep sub-category content from showing on the parent category page? For instance, let's say I have Blue Widgets and Blue Widgets With Whatchamajigs. I write a post about Whatchamajigs and the post preview shows up on BOTH the Blue Widgets AND the Blue Widgets With Whatchamajigs sub-category. I don't want it to show up on the main category though.

How do I fix this?

Thanks. Your blog rocks and it's in every one of my feed readers. ;-)

Pingback: Show the subcategories of current category. | Mommy Motivation

Your code works very well.
Is there a way to print all the subcategory names and, under each of them, a list of the posts included in the subcategory?

This is positively genius! I've been looking everywhere for something to do just this. Thank you SO much!

Does anyone know how to change the order by which the sub cats are displayed? It currently says 'id' but that's not alphabetical on my site. :(

I tried to use the code but was unable to get it to work. Can someone give me more insight as to where exactly and how to implement this code?? Thanks!!

where i should put this code?on the sidebar.php after what line??
thanks

Thanks so much. It's works great on my site.

Poor skills in php, unable to use code as it is, Please tell me how to use this code

i dont know why the code dont want to be pasted so i made php file and zipped.

you can download from here

Joost de Vald sorry for a lot of comments and if you like you can post the code right, thanks

Hello, i was looking for this and get working in sidebar when i'm looking on parent category.
When i'm click on childcategory they dont shows in sidebar. I saw few people asked same thing and SEO Scotland give us solution but, i have too many categories so, this solution with exact ID doesnt work for me. I will try to make but if somebody already made it please post.
Thank you!

WOW, this was faster than i was thinking, i made it!
So everybody who wants to show childrencategories even they click on childrencategory here is the code:

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&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>

you can see it working here, website is not ready yet. "biography" or "available works" are subcategories of artist.

ciao!

something goes wrong with code in last comment here is again.

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&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>

code is pasted wrong again, i will try again

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&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
if ($this_category) { ?>

Was ther a fix for this still found ? bcoz i also need this feature. on my blog .tried the codes, but the sub categories disappear when i click on it.

Hi Joost
I would love to download that php file that Rade posted on June 30th... It seems the link is not working.. Do you have another link?
Thanks

Thank you very much for this simple yet elegant solution. Saved me hours from messing around with template tags and/or queries.
BTW your site has a very appealing design.

Joost,
How do we reconcile the duplicate content issue when using subcategories? That is, when I assign a post to a subcategory (child of parent category) it is also visible in the parent category. Hence, wouldn't this be considered duplicate content?

Thanks for your time and insight, it is greatly appreciated!

Thank you.. I was looking for the same stuff. Thanx a lot.

Hi,

It had no problem till today. In homepage, it shows the subcategories of catid:190

I dunno why this happened. Cat id 190 is my new category and on the main page, it lists the sub cats of this cat as if I opened this cat page.

Why can it be?

Any ides? Thanks

Pingback: How to: Showing subcategories on WordPress category pages | Theme Heven

Pingback: ¿Existen plantillas en WordPress que permitan gestionar las categorías como páginas? « Oficio Gráfico

Hello, good post, very useful.

I just need help in one issue. How can I add a "follow" tag to these addes subcategories? because with the meta robots plugin, the subcategories within the category pages are setup as "nofollow". I just want to follow the subcategories of each category and no the other main categories.

Man man man! I love you :)
I was looking for it all day! Thank you so much! I'll use it for my wordpress category page.

Hello, this is very good. So I need to know where I have to insert the code and if it works with WordPress 2.8

Thanks a lot

Pingback: Migliora la Navigazione di WordPress con Breadcrumb e Sottocategorie

Great thread!!! It is almost working correctly for me. Two probelms, and I wonder if anyone has run into the same stuf or any help with modifying the php, using the example code at http://yoast.com/showing-subcategories-on-wordpress-category-pages/#comment-36688 (p.s. I know php when i look at it, but i am no coder)

1. Works when in sidebar.php, but includes a full category list on the homepage, as well as other pages. I only want this to show up on category archives.

2. I Tried to widgitize it so that I could use widget-logic to effect where this shows up, but as a widget it lists all categories, on every page, no matter where you are.

Any ideas?

hey, just had an epiphany and got this working right! I wanted to share in case anyone else runs into this:

1. I created a duplicate of sidebar.php and called it sidebar_categories.php.

2. Then I edited my archive.php file and wrapped the call to sidebar in a condition where if the archive type is category, than it will include the alternate sidebar instead.

Here is the code, placed at the bottom of the archive.php file instead of get_sidebar ():

if (is_category()) {
include (TEMPLATEPATH . "/sidebar_categories.php");
} else {
get_sidebar();
}

Problem solved without widgets. BTW, in my previous comment if forgot to mention I was using the plugin called 'Executable PHP widget' to try to turn the snippet into a widget.

Yoast is awesome.

Pingback: Showing subcategories on WordPress category pages - Yoast - Tweaking Websites « Social Media Greece

Pingback: Display Subcategories on Category Pages in WordPress

Pingback: Hookle » Display Subcategories on Category Pages in WordPress

Pingback: World On A Shoe String » Blog Archive » Working on photography.name1price.com

This is just what I was looking for, thank you for sharing!

I am searching for code or a plugin that will show the subcategories of a given category's parent. For example, if I have a parent category of "photos" and subcategories of "art," "friends," and "travel," how can I adjust your code so that if I am in the "photos" parent category OR in the "friends" subcategory archives, it will show clickable links for the "art," "friends," and "travel" sections in the sidebar? Thank you so much!! Very helpful!!

Great code Yoastie;-)

hi all, what is the codex for get list link post of the only subcategory and not category mother?
i would get list posts of the subcategory in archive.php page anyone can help me??

Comments closed, if you feel you have something to say:
drop me a line.