• Hi !
    I have this code in my sidebar.php template:

    $cat_ID = get_query_var('cat');
    echo '<ul>';
    wp_list_categories('title_li&hide_empty=0&child_of='.$cat_ID);
    echo '</ul>';

    which is showing children categories of current category.

    What I would like is to show children categories when there are any, but when there aren’t any child, to leave it blank and. Or anything else but “No categories” (this is the result of wp_list_categories function).
    I suppose some kind of if/else condition will solve that.
    I haven’t been able to make the solution so far …

    Any ideas, please ?
    Thnx

Viewing 10 replies - 1 through 10 (of 10 total)
  • Try:

    $cat_ID = get_query_var('cat');
    if( wp_list_categories('title_li&hide_empty=0&echo=0&child_of='.$cat_ID) ) {
    	echo '<ul>';
    	wp_list_categories('title_li&hide_empty=0&child_of='.$cat_ID);
    	echo '</ul>';
    }
    Thread Starter Micemade

    (@anydog)

    Hi Esmi, thnx for your input.
    No, that don’t works, but I understand your logic. It should work, but it does not.
    It still shows “No categories” …

    Thread Starter Micemade

    (@anydog)

    Yeah, still no luck …
    Btw, it’s obvious why your code, Esmi, doesn’t work. Because condition brackets are still returning something – in this case “No categories”.
    I tried with:

    $cat_ID = get_query_var('cat');
    $children=  get_categories('child_of='.$cat_ID) ;
    if ($children) {
    echo '<ul>';
    wp_list_categories('title_li&hide_empty=0&child_of='.$cat_ID);
    echo '</ul>';
    }

    , but still no luck …
    Heelp , pretty please … ??

    Try:

    $cat_ID = get_query_var('cat');
    $children=  get_categories('child_of='.$cat_ID) ;
    if ( $children && $children != 'No categories' ) {
    echo '<ul>';
    wp_list_categories('title_li&hide_empty=0&child_of='.$cat_ID);
    echo '</ul>';
    }
    Thread Starter Micemade

    (@anydog)

    Thanx Esmi … Nope ! No luck … ??

    I’m sorry but that code works for me within a category template file.

    Thread Starter Micemade

    (@anydog)

    Hmmm … Perhaps the problem lies in fact that I put that code in functions.php ?
    I found some issues when people tried to put get_categories in functions.php, but that was supposed to be solved with WP version 2.8 …
    I run this code on 2.9.2 , so it shuold work …

    Ok, I’ll try to put the code in my category template file …

    I’ll get back with the results …

    Thanks Esmi for all your effort … ??

    Thread Starter Micemade

    (@anydog)

    Addendum: I made a shorcode function in functions.php because I wanted to list child categories in current POST.
    Is there a way to add shortcode outside functions.php ?
    Here’s my code in it’s full:

    <?php
    function categories_in_post() {
    
    $cat_ID = get_query_var('cat');
    $children=  get_categories('child_of='.$cat_ID) ;
    if ( $children != 'No categories' ) {
    echo '<ul>';
    wp_list_categories('title_li&hide_empty=0&child_of='.$cat_ID);
    echo '</ul>';
    }
    }
    
    add_shortcode('cats', 'categories_in_post');
    ?>

    Aha! I hadn’t realised that you were placing this in functions.php. ??

    Thread Starter Micemade

    (@anydog)

    Well, still nothing …
    Esmi ?
    Anyone… ?

    Please ?
    Pretty please ? ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Current category children – conditional display’ is closed to new replies.