WordPress.org

Support

Support » How-To and Troubleshooting » Number of Parent Categories

Number of Parent Categories

  • 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

Viewing 1 replies (of 1 total)
  • 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
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Number of Parent Categories’ is closed to new replies.
Skip to toolbar