List all Subcategories

Adding this PHP code to your wordpress theme will display a list of only subcategories

    $echo = '<ul>' . "\n";
    $childcats = get_categories('child_of=' . $cat . '&hide_empty=1');

    foreach ($childcats as $childcat) {
        if (1 == $childcat->category_parent) {
            $echo .= "\t" . '<li><a href="' . get_category_link($childcat->cat_ID).'" title="' . $childcat->category_description . '">';
            $echo .= $childcat->cat_name . '</a>';
            $echo .= '</li>' . "\n";
    $echo .= '</ul>' . "\n";
    echo $echo;

Comments  Leave a Reply

  1. I am so good at messing out so here is a few questions/comments:

    Let’s say I have a main CAt called CARS and 3 sub-cats:
    1)Sports Cars
    2)Economic Cars
    3)4 x 4

    As we know now, if I have posts in cat #1 +2 +3, they will all show up under CARS – that is the standard in WP.

    Now with this code, if user click CAR he will get only the 3 sub cats WITHOUT any posts at all?

    Is this correct?

    Second question:

    Where do I add this code?



  2. Tweets that mention Wordpress List all Subcategories – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com January 26, 2011 at 6:29 pm

    […] This post was mentioned on Twitter by Y Combinator Newest!, HN Firehose. HN Firehose said: WordPress display list of only subcategories: http://bit.ly/geanqN […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!