0

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!

1
  • But you called it like this: ribbon_list_cats('lifestyle') ; in your template files (which should work)?
    – kaiser
    Commented Jul 15, 2011 at 12:20

1 Answer 1

0
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' );
5
  • Tried this before and didn't have any luck either.
    – Marc
    Commented Jul 15, 2011 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
    Commented Jul 15, 2011 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
    Commented Jul 15, 2011 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
    Commented Jul 15, 2011 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
    Commented Jul 15, 2011 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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