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.