WordPress.org

Ready to get started?Download WordPress

Forums

wp_list_categories() - How to show Subcategories or Categories of Parent (4 posts)

  1. Incubus66
    Member
    Posted 4 years ago #

    Hi all, I have one question.
    Can I add somewhere in wp_list_categories() condition, if are some subcategories then show them else show categories of the parent.
    Please Help, thanks :-)

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    How about something like:

    <?php
    if( wp_list_categories( 'child_of=' . $cat . '&echo=0' ) =='' ) {
    	// there are no child cats, so display parent cats
    	echo get_category_parents($cat, TRUE, ' &raquo; ');
    }
    else {
    	// we have child cats!
    	wp_list_categories( 'child_of=' . $cat)
    }
    ?>

    Untested and you might have to tweak it a little to get the display you want but that might get you started.

    http://codex.wordpress.org/Template_Tags/wp_list_categories
    http://codex.wordpress.org/Function_Reference/get_category_parents

  3. Incubus66
    Member
    Posted 4 years ago #

    Thanks for quick reply.
    But if I use it, it still returns 'No Categories' if there are no subcategories.

  4. Incubus66
    Member
    Posted 4 years ago #

    Ok, now It's working, I just must little change source code if wp_list_categories. Now I have last question, Can I somehow get ID of parent category?

Topic Closed

This topic has been closed to new replies.

About this Topic