List All Subcategories

Sometimes you just need a list of all child categories only and nothing else. The requirements of projects can be very diverse, as the solution can, and in my case it is a small code snippet that takes care of the output of Subcategories. Something for you to play, improve and expand. Enjoy!

$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 are closed.

3 comments

  1. Tomáš Kapler

    Ehm, maybe I'm stupid, but why don't you use http://codex.wordpress.org/Template_Tags/wp_list_categories? It got child_of as well.
    Or you mean to list only subcategories without parent categories, but mixed together? I would not much understand this need, but again it could be done with wplist categories and some simple regexp, what would probably be a bit faster than this.

  2. Alex

    Hi Tomáš, the code is only to display the subcategories, nothing more.

One pingback

  1. Here’s What We’re Reading – June 16, 2010 :: Warbux Creative