• My goal is to display conditional content in an archive page if:

    – Post belongs to the parent custom taxonomy and any subcategories of the same parent taxonomy.

    Let’s say, custom taxonomy is called custom_taxonomy and it has a category ‘Animals’ ID=01 (Parent)
    Subcategories of ‘Animals’ are ‘Beagle’ ID=02 and ‘Retriever’ ID=03.

    It’s easy with plain post categories and child subcategories.

    if (is_category('Animals') || cat_is_ancestor_of('Animals', get_query_var( 'cat' )) { echo 'Successs';
    

    How do I check whether the post belongs to a subcategory of taxonomy parent category ‘Animals’ ID=01?

    The first part works fine, but I have like 50 subcategories and I’m certain there should be a better way than listing them one by one.

    is_tax('custom_taxonomy', 'Animals') ) ||

Viewing 16 replies (of 16 total)
  • Instead of the second if(), you would use an elseif(). That way you will only get one of the statements executing. You can change the order of the checks around to suit what you want to come first.

    if (term_is_ancestor_of(448, $term, 'family') or is_term(448, 'family')) {
        echo '<p>Jacksons</p>';
    }
    elseif (term_is_ancestor_of(451, $term, 'family') or is_term(451, 'family')) {
        echo '<p>Watsons</p>';
    }
Viewing 16 replies (of 16 total)
  • The topic ‘Checking hierarchical taxonomy’ is closed to new replies.