One Serving

Healthy Portions, Healthy Diet
6 February, 2008 at 9:28 pm in Site News, , ,

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 ‘Weefz’s Show Post Subcategories‘ from the WordPress Plugins Directory.

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.3.3. It doesn’t do anything fancy, so should work with earlier versions. Comment below with your version number if you use it successfully, please. I’ll update the docs to list all tested 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 ;)



7 responses to “WordPress Plugin: Show Post Subcategories”

  1. Not bad! said:

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

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

    I thought “parent category id number” is automatic.

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

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

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

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

    Debbie,
    Thank you very much for your suggestion.

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

Leave a Reply

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>