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:

  1. Unzip the download into your WordPress plugins directory
  2. Activate it in the plugins management screen
  3. Go to your template editor
  4. Within The Loop, add the following tag
    <?php wfzShowPostSubcategories('prefix','suffix','separator','parent category id number') ?>
  5. Replace prefix, suffix and separators with the text of your choice, keeping it within the single quotation marks
  6. 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.


39 responses to “WordPress Plugin: Show Post Subcategories”

  1. Not bad!

    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

    April 4th, 2008 at 4:25 am, comment link
  2. Debbie

    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.

    April 4th, 2008 at 7:48 am, comment link
  3. Alfie

    I thought “parent category id number” is automatic.

    May 18th, 2008 at 8:48 am, comment link
  4. Debbie

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

    May 18th, 2008 at 10:12 am, comment link
  5. Mann

    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.

    July 11th, 2008 at 4:05 am, comment link
  6. Debbie

    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.

    July 11th, 2008 at 11:29 am, comment link
  7. Mann

    Debbie,
    Thank you very much for your suggestion.

    July 12th, 2008 at 3:39 am, comment link
  8. David

    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

    January 17th, 2009 at 9:04 pm, comment link
  9. Debbie

    You’re very welcome, David. Glad that I could help.

    January 18th, 2009 at 2:44 pm, comment link
  10. David

    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;

    January 27th, 2009 at 4:14 pm, comment link
  11. David

    Sorry here is relevant code snippet again:

    href='" . $categoryLink . "'>

    January 27th, 2009 at 4:17 pm, comment link
  12. Debbie

    Thanks David. I’m having a bit of trouble with Subversion at the moment but will add your fix when I next update.

    February 8th, 2009 at 9:18 pm, comment link
  13. itsameyer

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

    June 1st, 2009 at 8:47 pm, comment link
  14. Debbie

    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.

    June 2nd, 2009 at 10:01 am, comment link
  15. J

    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!

    June 4th, 2009 at 3:48 pm, comment link
  16. Debbie

    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

    June 9th, 2009 at 9:43 pm, comment link
  17. David

    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.

    June 23rd, 2009 at 2:57 am, comment link
  18. Debbie

    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.

    June 23rd, 2009 at 3:59 pm, comment link
  19. Webbando

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

    July 1st, 2009 at 9:07 pm, comment link
  20. Debbie

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

    July 2nd, 2009 at 7:40 pm, comment link
  21. Mark

    Still works beautifully in 2.9. Thanks for the elegant plugin.

    January 9th, 2010 at 11:20 pm, comment link
  22. Isaac Atia

    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.

    May 30th, 2010 at 5:53 pm, comment link
  23. Debbie

    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!

    May 31st, 2010 at 10:18 am, comment link
  24. Isaac Atia

    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 :)

    May 31st, 2010 at 3:17 pm, comment link
  25. Debbie

    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!

    May 31st, 2010 at 3:31 pm, comment link
  26. Isaac

    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?

    June 20th, 2010 at 9:53 pm, comment link
  27. Debbie

    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.

    June 20th, 2010 at 10:23 pm, comment link
  28. Isaac

    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/

    June 21st, 2010 at 12:52 am, comment link
  29. joe

    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

    June 23rd, 2010 at 6:50 pm, comment link
  30. Debbie

    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.

    June 23rd, 2010 at 6:55 pm, comment link
  31. joe

    That didn’t seem to work, the categories are still links. Did i edit the code correctly?
    $catList.= $category->cat_name . $separator;

    June 23rd, 2010 at 7:04 pm, comment link
  32. joe

    Apologies! that worked perfectly :) thanks for your “immediate” support

    June 23rd, 2010 at 7:17 pm, comment link
  33. Debbie

    No problem! You can usually catch me around this time, checking emails after work :)

    June 23rd, 2010 at 7:25 pm, comment link
  34. fahim

    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.

    October 14th, 2010 at 7:41 am, comment link
  35. Debbie

    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?

    October 14th, 2010 at 11:21 am, comment link
  36. Isaac

    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?

    December 28th, 2010 at 3:26 pm, comment link
  37. Debbie

    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.

    December 28th, 2010 at 6:46 pm, comment link
  38. Isaac

    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.

    January 5th, 2011 at 6:05 pm, comment link
  39. jam

    this so complicated ! i think ! i am trying to code it !

    March 23rd, 2011 at 10:52 am, comment link

Got something to say?

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>