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:


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

74 Responses

  1. MicrodesignBy Microdesign on 24 April, 2007

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

  2. Joost de ValkBy Joost de Valk on 24 April, 2007

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

  3. TextLinkCenterBy 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.

    Thanks

  4. Joost de ValkBy Joost de Valk on 3 May, 2007

    My pleasure :)

  5. TechDukeBy 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.

  6. TechDukeBy 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 :)

  7. Joost de ValkBy Joost de Valk on 15 June, 2007

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

  8. TechDukeBy 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.

  9. Joost de ValkBy Joost de Valk on 15 June, 2007

    Good, nice to know :) thx!

  10. TechDukeBy 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.

    TechDuke

  11. Joost de ValkBy Joost de Valk on 15 June, 2007

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

  12. TechDukeBy TechDuke on 15 June, 2007

    Thank you VERY much. Let me test it :)

  13. Joost de ValkBy Joost de Valk on 15 June, 2007

    Cool :)

  14. lapsaJBy lapsaJ on 17 June, 2007

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

  15. Joost de ValkBy Joost de Valk on 17 June, 2007

    lapsaJ: does it have subcategories? :)

  16. lapsaJBy lapsaJ on 17 June, 2007

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

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

  18. Joost de ValkBy 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 :)

  19. lapsaJBy lapsaJ on 19 June, 2007

    Thanks Joost for all your help! :)

  20. KirstenBy 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?

  21. RichBy Rich on 21 July, 2007

    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.

  22. n37By n37 on 22 July, 2007

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

  23. RajBy Raj on 1 August, 2007

    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

  24. Joost de ValkBy 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.

  25. SEO ScotlandBy 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′); ?>

    Cheers!
    Iain

  26. EmanoelBy 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…

  27. RayBy Ray on 12 September, 2007

    I have the exact same problem as Kirsten!

  28. SEO ScotlandBy SEO Scotland on 12 September, 2007

    Ray & Kirsten – see my post above using category_parent.

  29. tonyBy 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.

  30. takienBy 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

  31. CathyBy 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!
    Cathy

  32. EssenzZoBy 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?

  33. Rapidshare BloggersBy Rapidshare Bloggers on 13 January, 2008

    Great ! Thanks for the Share :)

  34. namakamuBy namakamu on 10 February, 2008

    can anyone show me the demo?
    thanks

  35. benBy 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
    ben

  36. Daniel MiranteBy 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…

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

  38. EverettBy Everett on 18 March, 2008
  39. AlbertoBy 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?

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

  41. Ryan GBy 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!!

  42. egiBy egi on 6 June, 2008

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

  43. PualBy Pual on 17 June, 2008

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

  44. dheeraj singhBy 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

  45. RadeBy 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

  46. RadeBy 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!

  47. RadeBy 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:

    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!

  48. RadeBy Rade on 30 June, 2008

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

  49. RadeBy Rade on 30 June, 2008

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

  50. vijaleshBy 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.

  51. TrevinoBy 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?
    Thanks

  52. RadeBy Rade on 12 September, 2008
  53. MikiBy 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.

  54. KevinBy Kevin on 20 October, 2008

    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!

  55. Web Designer IndiaBy Web Designer India on 13 November, 2008

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

  56. TuzlaBy Tuzla on 26 April, 2009

    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

  57. JavierBy 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.

  58. Yigit OzdamarBy 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.

  59. WebbandoBy 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

  60. mosaBy 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 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?

  61. mosaBy 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 {
    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.

  62. GabyBy Gaby on 30 September, 2009

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

  63. wildbugBy 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!!

  64. Oliver NielsenBy Oliver Nielsen on 20 October, 2009

    Great code Yoastie;-)

  65. trauBy 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??

Trackbacks