Number of Parent Categories (2 posts)

  1. Net Zero
    Posted 5 years ago #


    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?


  2. stvwlf
    Posted 5 years ago #

    One idea from

    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