Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.