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!



by Microdesign on 24 April, 2007 at 10:19
Cool thanks joost, i was searching for something like this.
by Joost de Valk on 24 April, 2007 at 11:26
Cool, don't hesitate to shout out other things you'd like covered regarding WordPress!
by TextLinkCenter on 3 May, 2007 at 05:43
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
by Joost de Valk on 3 May, 2007 at 09:08
My pleasure :)
by TechDuke on 15 June, 2007 at 05:59
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.
by TechDuke on 15 June, 2007 at 06:17
I figured it out myself. There was a problem with current template, by replacing some code lines with default theme sidebar, it worked. Thanks :)
by Joost de Valk on 15 June, 2007 at 08:44
Hey TechDuke, nice to know you figured it out :) Was anything wrong in my code?
by TechDuke on 15 June, 2007 at 10:10
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.
by Joost de Valk on 15 June, 2007 at 10:13
Good, nice to know :) thx!
by TechDuke on 15 June, 2007 at 10:23
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
by Joost de Valk on 15 June, 2007 at 10:25
It's the fix for WordPress pages going supplemental :)
by TechDuke on 15 June, 2007 at 11:45
Thank you VERY much. Let me test it :)
by Joost de Valk on 15 June, 2007 at 11:53
Cool :)
by lapsaJ on 17 June, 2007 at 10:33
Tried this, unfortunately no categories show at all. Any ideas?
by Joost de Valk on 17 June, 2007 at 15:08
lapsaJ: does it have subcategories? :)
by lapsaJ on 17 June, 2007 at 15:53
Yeah. I have one category which has a category inside that, and another category inside that.
by n37 on 19 June, 2007 at 08:41
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. =)
by Joost de Valk on 19 June, 2007 at 08:49
hi n37, mail me at joost at joostdevalk dot nl and I'll see what I can do to help :)
by lapsaJ on 19 June, 2007 at 14:09
Thanks Joost for all your help! :)
by Kirsten on 12 July, 2007 at 18:18
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?
by Rich on 21 July, 2007 at 06:48
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.
by n37 on 22 July, 2007 at 03:59
@rich, try with some CSS / DHTML code instead =)
by Raj on 1 August, 2007 at 17:16
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
by Joost de Valk on 1 August, 2007 at 22:40
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.
by SEO Scotland on 13 August, 2007 at 20:22
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
by Emanoel on 6 September, 2007 at 04:20
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...
by Ray on 12 September, 2007 at 07:12
I have the exact same problem as Kirsten!
by SEO Scotland on 12 September, 2007 at 10:13
Ray & Kirsten - see my post above using category_parent.
by tony on 22 September, 2007 at 08:54
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.
by takien on 17 November, 2007 at 01:20
Thanks for the code..
but i don't know which file i should put the code in...
Help please. thanks
by Cathy on 7 December, 2007 at 08:35
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
by EssenzZo on 24 December, 2007 at 19:03
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?
by Rapidshare Bloggers on 13 January, 2008 at 07:40
Great ! Thanks for the Share :)
by namakamu on 10 February, 2008 at 00:12
can anyone show me the demo?
thanks
by ben on 14 February, 2008 at 10:31
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
by Daniel Mirante on 8 March, 2008 at 15:30
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...
by Everett on 18 March, 2008 at 02:06
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. ;-)
by Everett on 18 March, 2008 at 02:08
Nevermind - Found It:
http://lonewolf-online.net/computers/knowledgebase/wordpress-not-show-posts-of-subcategory/
Thanks anyway though!
Pingback: Show the subcategories of current category. | Mommy Motivation
by Alberto on 26 March, 2008 at 18:21
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?
by Ally on 17 April, 2008 at 13:25
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. :(
by Ryan G on 4 June, 2008 at 17:49
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!!
by egi on 6 June, 2008 at 19:17
where i should put this code?on the sidebar.php after what line??
thanks
by Pual on 17 June, 2008 at 06:46
Thanks so much. It's works great on my site.
by dheeraj singh on 28 June, 2008 at 14:54
Poor skills in php, unable to use code as it is, Please tell me how to use this code
by Rade on 30 June, 2008 at 02:00
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
by Rade on 30 June, 2008 at 01:24
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!
by Rade on 30 June, 2008 at 01:48
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!
by Rade on 30 June, 2008 at 01:49
something goes wrong with code in last comment here is again.
by Rade on 30 June, 2008 at 01:51
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) { ?>
by vijalesh on 31 July, 2008 at 07:19
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.
by Trevino on 12 September, 2008 at 20:19
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
by Rade on 12 September, 2008 at 20:44
Hello Trevino,
here is the link again http://www.zedesino.com/wp_custom_scripts/childcategory-in-sidebar.zip
by Miki on 26 September, 2008 at 16:09
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.
by Kevin on 20 October, 2008 at 07:41
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!
by Web Designer India on 13 November, 2008 at 07:40
Thank you.. I was looking for the same stuff. Thanx a lot.
by Tuzla on 26 April, 2009 at 16:02
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
by Javier on 7 June, 2009 at 14:46
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.
by Yigit Ozdamar on 16 June, 2009 at 21:24
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.
by Webbando on 2 July, 2009 at 19:16
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
by mosa on 28 July, 2009 at 07:06
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?
by mosa on 28 July, 2009 at 07:31
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
by Gaby on 30 September, 2009 at 14:45
This is just what I was looking for, thank you for sharing!
by wildbug on 14 October, 2009 at 19:47
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!!
by Oliver Nielsen on 20 October, 2009 at 16:04
Great code Yoastie;-)
by trau on 16 November, 2009 at 16:24
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??