Determine if Term has Grandparent/Great-Grandparent

I have a custom taxonomy (hierarchical) for which I’m outputting the results. For example’s purpose:

–Drinks

Read More

—-Tequila

——Reposado

——Blanco

—-Beer & Wine

——Beer

——Wine

——–Red

——–White

——–Rose

—-Whisky

——Lowlands

——Highlands

——Islay

I have a loop that goes through and outputs the hierarchy–the problem is that I have ONE term that goes a level deeper than the rest (the different types of wine: red, white, rose). Currently, my loop outputs headings for each term, so the loop puts Red, White, Rose at the same level as the rest of the terms. What I need it to do is realize when a term is 3-levels deep, and instead output an so that I can maintain the correct hierarchy.

All that to say: is there a way to determine a term’s depth? if $term_depth = 4, then do this. If $term_depth = 4, then do this?

My loop:

$theCatId = get_term_by( 'slug', $currentslug, 'drink_cats' );
$termID = $theCatId->term_id;
$taxonomyName = 'drink_cats';
$termchildren = get_term_children( $termID, $taxonomyName );
$termhier = _get_term_hierarchy($taxonomyName);

if (isset($termhier[$termID])) {                
    foreach ($termchildren as $child) {
        $term = get_term_by( 'id', $child, $taxonomyName );
        $termname = $term->name;
        // loop here to get $term depth?
        // level 3
        echo '<h2>'. $termname . '</h2>';
        // level 4 <h3>greatgrandchild</h3>
        $args = array(
            'post_type' => 'menu_drinks',
            'orderby' => 'menu_order',
            'order' => 'ASC',
            'posts_per_page' => -1,
            'taxonomy'  => 'drink_cats',
            'term'      => $term->slug
        );
        $drink_query = get_posts( $args );
        if ($drink_query) {
            echo '<ul class="drink-group">';
            foreach( $drink_query as $post ) {   
                setup_postdata($post); 
                $drinkdesc = get_field('drink_description');
                $drinkprice = get_field('drink_price');
                $drinkphoto = get_field('drink_photo');
                //output specific drink, description, price
                ?>

                <li>
                    <h4><?php the_title(); if ($drinkprice) { echo " - $" . $drinkprice; } ?></h4>
                    <?php if ($drinkdesc) { ?>
                        <p><?php echo $drinkdesc; ?></p>
                    <?php }?>                                
                </li>

                <?php }
        echo '</ul>';
        }
    }
}

else { 
    $args = array(
        'post_type' => 'menu_drinks',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'taxonomy'  => 'drink_cats',
            'term' => $currentpage
    );

    $drink_query = get_posts( $args );
    echo '<ul class="drink-group">';
        foreach( $drink_query as $post ) :   
            setup_postdata($post); 
            $drinkdesc = get_field('drink_description');
            $drinkprice = get_field('drink_price');
            $drinkphoto = get_field('drink_photo');
        ?>

                    <li>
                            <h4><?php the_title(); if ($drinkprice) { echo " - $" . $drinkprice; } ?></h4>
                            <?php if ($drinkdesc) { ?>
                <p><?php echo $drinkdesc; ?></p>
            <?php } ?>                                
                    </li>

        <?php endforeach; 
    echo '</ul>';       
}

Thanks for you time–if anyone is interested, I can send a link to the dev server so you can see how the Beer & Wine page outputs now, if this is unclear.

Related posts

Leave a Reply

1 comment

  1. Use get_ancestors() and count the returned array: that’s the number of ancestors a term has.

    /**
     * Count a term’s ancestors.
     *
     * @param  int    $term_id
     * @param  string $taxonomy
     * @return int
     */
    function wpse_57512_count_ancestors( $term_id = FALSE, $taxonomy = FALSE )
    {
        if ( FALSE === $term_id and ! empty ( get_queried_object()->taxonomy ) )
        {
            $term_id  = get_queried_object_id();
            $taxonomy = get_queried_object()->taxonomy;
        }
    
        $ancestors = get_ancestors( $term_id, $taxonomy );
    
        return $ancestors ? count( $ancestors ) : 0;
    }
    

    For a practical use case you may take a look at my plugin T5 Parent Terms in body_class. Its name should tell you what it does. 🙂