'How to I show only parent and first child with get_the_terms?

I have an issue with showing custom taxonomy categories on my page. I wish to show parent category and child category(hide all grandchildren). I've tried to use child_of selector but that doesn't work here.

As I var_dump my variable there is no way of knowing which one is granchild as it only shows parent category.

-Category <--- I want to show this

---Child Category <--- I want to show this

------Grandchild Category <--- I don't want to show this

 <?php
    $single_woodtype_terms = get_the_terms(
        $product->get_id(),
        'gallery_woodtype_taxonomy',
    );

    if ($single_woodtype_terms) : ?>

        <div class="single-product-details__item">

            <b><?= __('WOOD', 'thermory'); ?></b>
            <ul>
                <?php
                
                foreach ($single_woodtype_terms as $single_woodtype_term) :
                    $single_woodtype_cat = $single_woodtype_term->name;
                ?>
                    <li>
                        <?= $single_woodtype_cat; ?>
                    </li>
                <?php endforeach; ?>
            </ul>

        </div>

    <?php endif; ?>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source