Showing subcategories on WordPress category pages

April 23rd, 2007 – 74 Comments

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>";
    echo "</ul>";

Have fun with it!

74 Responses to Showing subcategories on WordPress category pages

  1. trau
    By trau on 16 November, 2009

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

  2. Oliver Nielsen
    By Oliver Nielsen on 20 October, 2009

    Great code Yoastie;-)

  3. wildbug
    By wildbug on 14 October, 2009

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

  4. Gaby
    By Gaby on 30 September, 2009

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

  5. mosa
    By mosa on 28 July, 2009

    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 {

    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.

  6. mosa
    By mosa on 28 July, 2009

    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 (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?

  7. Webbando
    By Webbando on 2 July, 2009

    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

  8. Yigit Ozdamar
    By Yigit Ozdamar on 16 June, 2009

    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.

  9. Javier
    By Javier on 7 June, 2009

    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.

  10. Tuzla
    By Tuzla on 26 April, 2009


    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

  11. Web Designer India
    By Web Designer India on 13 November, 2008

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

  12. Kevin
    By Kevin on 20 October, 2008

    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!

  13. Miki
    By Miki on 26 September, 2008

    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.

  14. Rade
    By Rade on 12 September, 2008
  15. Trevino
    By Trevino on 12 September, 2008

    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?

  16. vijalesh
    By vijalesh on 31 July, 2008

    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.

  17. Rade
    By Rade on 30 June, 2008

    code is pasted wrong again, i will try again

    $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) { ?>

  18. Rade
    By Rade on 30 June, 2008

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

    $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) { ?>

  19. Rade
    By Rade on 30 June, 2008

    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:

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


  20. Rade
    By Rade on 30 June, 2008

    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!

  21. Rade
    By Rade on 30 June, 2008

    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

  22. dheeraj singh
    By dheeraj singh on 28 June, 2008

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

  23. Pual
    By Pual on 17 June, 2008

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

  24. egi
    By egi on 6 June, 2008

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

  25. Ryan G
    By Ryan G on 4 June, 2008

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

  26. Ally
    By Ally on 17 April, 2008

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

  27. Alberto
    By Alberto on 26 March, 2008

    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?

  28. Everett
    By Everett on 18 March, 2008
  29. Everett
    By Everett on 18 March, 2008

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

  30. Daniel Mirante
    By Daniel Mirante on 8 March, 2008

    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…

  31. ben
    By ben on 14 February, 2008

    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

  32. namakamu
    By namakamu on 10 February, 2008

    can anyone show me the demo?

  33. Rapidshare Bloggers
    By Rapidshare Bloggers on 13 January, 2008

    Great ! Thanks for the Share :)

  34. EssenzZo
    By EssenzZo on 24 December, 2007

    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?

  35. Cathy
    By Cathy on 7 December, 2007

    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!

  36. takien
    By takien on 17 November, 2007

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

    Help please. thanks

  37. tony
    By tony on 22 September, 2007

    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.

  38. SEO Scotland
    By SEO Scotland on 12 September, 2007

    Ray & Kirsten – see my post above using category_parent.

  39. Ray
    By Ray on 12 September, 2007

    I have the exact same problem as Kirsten!

  40. Emanoel
    By Emanoel on 6 September, 2007

    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…

  41. SEO Scotland
    By SEO Scotland on 13 August, 2007

    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′); ?>


  42. Joost de Valk
    By Joost de Valk on 1 August, 2007

    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.

  43. Raj
    By Raj on 1 August, 2007


    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.

  44. n37
    By n37 on 22 July, 2007

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

  45. Rich
    By Rich on 21 July, 2007


    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.

  46. Kirsten
    By Kirsten on 12 July, 2007

    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?

  47. lapsaJ
    By lapsaJ on 19 June, 2007

    Thanks Joost for all your help! :)

  48. Joost de Valk
    By Joost de Valk on 19 June, 2007

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

  49. n37
    By n37 on 19 June, 2007

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

  50. lapsaJ
    By lapsaJ on 17 June, 2007

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

  51. Joost de Valk
    By Joost de Valk on 17 June, 2007

    lapsaJ: does it have subcategories? :)

  52. lapsaJ
    By lapsaJ on 17 June, 2007

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

  53. Joost de Valk
    By Joost de Valk on 15 June, 2007

    Cool :)

  54. TechDuke
    By TechDuke on 15 June, 2007

    Thank you VERY much. Let me test it :)

  55. Joost de Valk
    By Joost de Valk on 15 June, 2007

    It’s the fix for WordPress pages going supplemental :)

  56. TechDuke
    By TechDuke on 15 June, 2007

    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.


  57. Joost de Valk
    By Joost de Valk on 15 June, 2007

    Good, nice to know :) thx!

  58. TechDuke
    By TechDuke on 15 June, 2007

    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.

  59. Joost de Valk
    By Joost de Valk on 15 June, 2007

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

  60. TechDuke
    By TechDuke on 15 June, 2007

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

  61. TechDuke
    By TechDuke on 15 June, 2007

    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.

  62. Joost de Valk
    By Joost de Valk on 3 May, 2007

    My pleasure :)

  63. TextLinkCenter
    By TextLinkCenter on 3 May, 2007

    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.


  64. Joost de Valk
    By Joost de Valk on 24 April, 2007

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

  65. Microdesign
    By Microdesign on 24 April, 2007

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

Check out our must read articles about Analytics