In order to run this site, I need to display the subcategories of a particular category in single post pages. There are a few other people who were also looking to do the same thing, but got no joy. I wrote my own very basic plugin to do the job.
Download version 0.3 ‘Weefz’s Show Post Subcategories‘ from the WordPress Plugins Directory (or see below for version 0.4).
Here’s how it works:
- Unzip the download into your WordPress plugins directory
- Activate it in the plugins management screen
- Go to your template editor
- Within The Loop, add the following tag
<?php wfzShowPostSubcategories('prefix','suffix','separator','parent category id number') ?> - Replace prefix, suffix and separators with the text of your choice, keeping it within the single quotation marks
- Replace cat_ID with the ID number of the parent category
For example, say that you want to show the post’s subcategories of category 5 in an unordered list. Use the following tag:
<?php wfzShowPostSubcategories('<ul><li>','</ul>','</li><li>','5') ?>
I have used this with all versions of WordPress from 2.2.1 to 2.7.0. It doesn’t do anything fancy, so should work with earlier versions.
Disclaimer: This is my first WordPress plugin, so please test it out before installing on a live site. I use it myself right here at One Serving – it drives the links to the “High in” nutrient fields for every food serving size post.
Drop me a comment below with any questions or advice. If you like the plugin, feel free to buy me a beer or healthy juice drink with the PayPal button ;)
Update 8th June 2009: Plugin is now on version 0.4. By popular request, it now allows you to display subcategories without any separator. Just use the following:
<?php wfzShowPostSubcategories('','','','your parent category id') ?>
It’s temporarily available here until I find the time to upload it properly to the WordPress Plugins directory.

I like this plug-in except that I can’t get it to work like your example. When I implement the plug-in, it spits out another subcategory title for the number of posts that I have in said subcategory.
In other words, if I have 3 posts in subcategory X and 2 in subcategory Y, implementing your plug-in returns something like this:
subcategory X
subcategory X
subcategory X
subcategory Y
subcategory Y
I’m not sure I understand properly. Can you tell me what version of WP you’re using, and post the code that you’re using to call the plugin, please? Remove the < ? and ?> from the end of the line and it should display here with no problems.
I thought “parent category id number” is automatic.
Alfie, not in this plugin.
You might have a post that falls under 4 categories, 2 that are child categories of A and two that are child categories of B. This plugin will allow you to display only the child categories of A (or B).
Hi Debbie,
I look at your website, I really like your layout in the category, may you please help me how to setup getting child categories display in the parent category page. I did download and activate your plugin but still don’t know how to make it work. I really appreciate your help in advance.
Hi Mann. This plugin isn’t intended to alter category page layouts. It’s intended for use within the body of a single post page.
Try the WordPress.org Codex or ask in the WordPress forums. Most of my category layout problems have usually already been discovered and solved by someone else.
Debbie,
Thank you very much for your suggestion.
Debbie – THANK YOU!!! For days I have been searching for solution to this problem once I found how limited the_category was. Such an elegant solution from you. Love your site too.
I’ll be donating for sure.
David
You’re very welcome, David. Glad that I could help.
Debbie – One quick fix. I noticed page was not XHTML valid because links were not enclosed with “.
This change seems to have fixed things (adds extra ‘ and ‘):
$catList.="" . $category->cat_name . "" . $separator;Sorry here is relevant code snippet again:
href='" . $categoryLink . "'>Thanks David. I’m having a bit of trouble with Subversion at the moment but will add your fix when I next update.
Hi this plugin is exactly what i was looking for :D But is there a way to output a diferent code if the post isnt listed in any of the specified subcategories? Now it just outputs And it should output a message of som sort. :)
Hi itsameyer,
It doesn’t currently do that. I will look into providing an option for a customisable message/html output if your post is not in the selected category.
Hi Debbie,
This is a perfect solution to what I needed as well — many thanks.
One minor issue I’m having that I’m wondering if you could help solve: I need to display the child category name without any prefix or suffix — just the name by itself.
If I try setting those fields blank — i.e. wfzShowPostSubcategories(”,”,”,’3′) — then the code doesn’t work. But anything I place inside those fields, of course, will end up being displayed…which is what I don’t want.
Any idea for a workaround on that?
Thanks again!
Hi all,
I’ve made the changes requested by David and J. Not posted it yet to WordPress Plugin Directory but it is available from this site. See the update in the post above for the link.
itsameyer, I’m still looking into your request. Will let you know.
– Debbie
Hi Debbie – It’s David again. Have you tried plugin with 2.8 yet? I’m getting error, but I’m not certain its this plugin. I’ll keep testing.
Not tried 2.8 yet, no. I just had a quick look at the 2.8 ChangeLog and there don’t seem to be any changes made to get_the_category so it shouldn’t affect this plugin.
Shouldn’t…
Let me know if you track it down to this, please.
Hi, I tried with WordPress 2.8.1-beta2 and I cannot see all subcategories. Where Must I insert the code? In Tried in the sidebar but it doesn’t work :(
Hi Webbando.
It’s not a plugin for the sidebar – it’s designed to show subcategories within a single post page. You’ll need to place the code somewhere within the post section of your template – within The Loop
It sounds like you might be looking for something like wp_list_categories(‘child_of=YourParentCategoryIDHere’);
Still works beautifully in 2.9. Thanks for the elegant plugin.
Hello Debbie,
I have inserted the code as is into my index.php and my front page has this on top: prefixsuffix?>
I’ve tried countless plugins and not sure on how to solve this problem. What I basically want to get done is have the band’s name (Aerosmith) show up as SubCategory when people click the Parent Category (A) instead of the song (post) I Don’t Want To Miss A Thing.
This will be major help.
Hey Isaac,
Not sure I can help you with this. My plugin doesn’t have the phrase “prefixsuffix?>” in it at all, so I don’t know where that’s being generated.
I had a look at your site. Are you trying to use this for the alphabet navigation bar and the Category sidebar? This plugin is designed only to work within individual posts.
I think what you want is a custom category template within your theme:
Category Templates
You could either do 26 hardcoded category pages for A – Z or try some fancy detection to identify when a category is a parent and react accordingly. There may be some category-specific plugins that can help you there.
Best of luck!
Debbie,
I appreciate the prompt response. I looked at the link you gave me but sometimes PHP is like chinese to me, I only know HTML.
Yes, I’m trying to use it for the alphabet navigation bar and category sidebar, which is not a problem because the sidebar letters will show up as soon as I make a post for that letter. However, my problem is that when I click “A” for example, I want visitors to see a list of the subcategories (artists) like:
Abba
Aerosmith
Ashanti
Right now it just shows the post right away..which is not what I want, another example: I want to click on a letter B and see the subcats like Boys Like Girls, Boys 2 Men, Beyonce, Etc.
You sorta have what I want in your theme. Your have allergens and then a big list, food group and then a big list, which is what ive been trying to achieve for months.
If you can give me a code example of what I should do, it’d be great because then I can do the rest.
If you can help, I’d love to make a generous donation :)
Hi again, Isaac!
Thing is, what you’re trying to do is a little outside the traditional WordPress structure so you’ll need someone who understands writing themes and using template tags. Thanks for your generous offer; I’m not exactly an expert myself ;)
I’m gonna point you towards the WordPress Themes and Templates support forum. Best of luck!
Hi again Debbie,
I finally got what I wanted done…Subcats are finally showing (Aerosmith under A) but now when clicking the artist it says No Categories and wont connect the song to the artist
Would you know by any chance what code I can use in order to link the song (post) to the artist (subcat) ?
I used this code by the way in order to show subcats after a whole week of searching the internet but i cant find how to link the post to the subcategory..any idea?
Hey Isaac. Not sure how your blog’s set up but by far the easiest way to do this is to use the Categories thing on the right-hand side of the Add New/Edit Post page to select them for your post.
You can either create new categories directly in that widget or use the main Categories management page at yourblognameaddress/wp-admin/categories.php Don’t forget to set the parent category accordingly.
Debbie, I apologize for leaving so many of my comments on your page..
I do know how to do everything youve mentioned. My thing is I have this post listed under the subcat Aerosmith but when I go to letter A and then click Aerosmith it says “No categories” instead of displaying a list of the posts under the subcat “Aerosmith”. Is there a code which I can use to have the posts show up for the category (“I dont want to miss a thing” when you click Aerosmith? As you can see, I do have the post assigned to the subcat through this link: http://themlyrics.com/a/aerosmith/i-dont-want-to-miss-a-thing/
Hi Debbie,
great plugin, just wondering is there a way to show categories as text instead of a link? so they display but don’t link to category pages.??
Thanks
You could edit the plugin itself. Line 58, replace $catList.=”” . $category->cat_name . “” . $separator; with $catList.= $category->cat_name . $separator;
That should work, though obviously it would remove the link for all calls to the template tag across your whole blog.
That didn’t seem to work, the categories are still links. Did i edit the code correctly?
$catList.= $category->cat_name . $separator;
Apologies! that worked perfectly :) thanks for your “immediate” support
No problem! You can usually catch me around this time, checking emails after work :)
Hi,
Nice plugin dude.
I installed your plugin in wordpress 3. But It is displaying all blank values could you please write a complete tutorial for this. My client needs it urgently.
Hi Fahim,
This a complete tutorial – there’s literally nothing else to do for the plugin. As long as your post is in one or more sub-categories of a parent category then it should show all the sub-categories.
Do you have a test example that I can see online, perhaps?
Debbie, I wanted to thank you for all your help in the past. It has helped me tremendously to get my web affairs in check. You can check out my site now http://www.themlyrics.com, it’s definitely up and running now. However, I found something weird happening. If you to Artists by the letter ‘D’ (http://themlyrics.com/category/d/) then they are all listed fine but if I can go letter T (http://themlyrics.com/category/t/) it lists the posts instead of the sub category. Any idea how to fix this?
Hey Isaac. Site’s looking pretty slick now. Well done! Hard to say the cause without looking at your implementation – am I right in thinking you have separate category templates for letters A through D? You’d need to do copy those templates for the rest of the letter parent categories.
Debbie,
I simply use a WordPress theme that I found online after searching for weeks and months. I only changed some text here and there, but none of the code. I really wish that I could figure it out. If you have some time, do you think if I emailed you my login info to my ftp you could take a look at it? By the way, when I make a partner page next week I’m going to link to your site.
this so complicated ! i think ! i am trying to code it !