WordPress.org

Ready to get started?Download WordPress

Forums

Number of Parent Categories (2 posts)

  1. Net Zero
    Member
    Posted 3 years ago #

    Hey,

    how do I request the number of parent categories of a category?

    I want to do the following:

    If the category has only a parent category (Sports > Soccer) I want to display text: If you like soccer you might like the following types of soccer as well.

    If the category has a grandparent category (Sports > Soccer > Soccer Tennis) I want to diplay a different text.

    How can I do this?

    Thanks

  2. stvwlf
    Member
    Posted 3 years ago #

    One idea from
    http://codex.wordpress.org/Template_Tags/get_category_parents

    this function call
    <?php echo get_category_parents($cat, TRUE, ' || '); ?>
    returns a string of links for breadcrumbs, separated by ||
    Internet || Blogging || WordPress ||

    If you run that function on the category you are checking you could count the # of occurrences of '||' in the function's return string. If it is more than two then you know there is a grandparent category.

    $cat_list = get_category_parents($cat, TRUE, ' || ');
    if ( substr_count( $cat_list, '||' ) > 2 ) {
       // grandparent code here
    } else {
       // "no grandparent" code here
    }

Topic Closed

This topic has been closed to new replies.

About this Topic