WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here's my custom function to list child categories by the parent slug.

function ribbon_list_cats($parent_cat_slug)
    $parent_category = get_category_by_slug($parent_cat_slug);
    if ($parent_category->count != 0) {
        echo "<ul>";
        echo wp_list_categories('title_li=&child_of='.$parent_category->term_id.'&hide_empty=0&show_option_none=&echo=0');
        echo "</ul>";

I'm having trouble getting it to return and output. It doesn't throw an error. I'm using it multiple times so I created this but when I do it like so:

$parent_category = get_category_by_slug('lifestyle');
if ($parent_category->count != 0) {
    echo "<ul>";
    echo wp_list_categories('title_li=&child_of='.$parent_category->term_id.'&hide_empty=0&show_option_none=&echo=0');
    echo "</ul>";

right in the template it works fine. But that's a lot of code to repeat over and over again.

Hope that makes sense. Thanks!

share|improve this question
But you called it like this: ribbon_list_cats('lifestyle') ; in your template files (which should work)? – kaiser Jul 15 '11 at 12:20
up vote 0 down vote accepted
function ribbon_list_cats( $parent_cat_slug, $echo = true )
    $parent_category = get_category_by_slug( $parent_cat_slug );
    if ( $parent_category->count > 0 ) 
        $output  = '<ul>';
        $output .= wp_list_categories('title_li=&child_of='.$parent_category->term_id.'&hide_empty=0&show_option_none=&echo=0');
        $output .= '</ul>';
    // Debug: uncomment the following line
    // echo '<pre>'; var_dump( $output ); echo '</pre>';

    if ( $echo === false )
        return $output;

    return print $output;

// Call it like this:
ribbon_list_cats( 'uncategorized' );
share|improve this answer
Tried this before and didn't have any luck either. – Marc Jul 15 '11 at 12:25
Please use the debug I built into your function and tell what's happening. "didn't have any luck either" is the opposite of meaningful and gives no information about what's happening on your screen - and guessing is not my favorite thing. – kaiser Jul 15 '11 at 12:29
Whow sorry man. Glad people come here for help get responses like this. I'm getting a output of "NULL". I'm giving you everything I got man and there's no "guessing". – Marc Jul 15 '11 at 13:04
When I said "didn't have any luck either" sorry I wasn't clearer but I figured it was clear that I was having the same output... which was no output. Nevertheless I removed the conditional statement regarding the count. So it's not getting the count from $parent_category->count. – Marc Jul 15 '11 at 13:11
First, "no output" can mean a lot and doesn't neccesarily mean null. I updated the function (forgot to .= for $output), tested it and it works without a problem. – kaiser Jul 15 '11 at 13:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.