0

I'm using a custom taxonomy called 'product_cat' and post type called 'product', I'm trying to display all parent terms, child terms and grandchild terms within the taxonomy.

So I get a list like this, But I only want the grandchild terms to be displayed if I'm on the child page.

Courses (parent)
- Industry (child)
-- Industry One (grandchild)
-- Industry Two (grandchild)
-- Industry Three( grandchild)
- Trade (child)
-- Trade One (grandchild)
-- Trade Two (grandchild)
-- Trade Three (grandchild)

Essential Packages (parent)
- Construction (child)
-- Construction One (grandchild)
-- Construction Two (grandchild)
-- Construction Three (grandchild)

I'm currently using this snippet to try and help me but its not working the way I'm wanting it to.

<?php
    $taxonomyName = "product_cat";   
    $parent_terms = get_terms($taxonomyName, array('include' =>'67', 'orderby' => 'slug', 'hide_empty' => false));   
    echo '<ul>';
        foreach ($parent_terms as $pterm) {
            echo '<li>';
                echo '<a href="#">'.$pterm->name.'</a>'; // Child 
                $terms = get_terms($taxonomyName, array( 'child_of' =>'67', 'orderby' => 'slug', 'hide_empty' => false));
                echo '<ul class="children">';
                    foreach ($terms as $term) {
                        echo '<li><a href="'.get_term_link( $term->name, $taxonomyName ) . '">'.$term->name.'<span class="count">'.$term->count.'</span></a></li>';  // GrandChild
                    }
                echo '</ul>'; 
            echo '</li>'; 
        }
    echo '</ul>';
?>

If anyone could help me out and point me into the right direction I'd really appreciate all the help.

1

get_terms(); accept include as an array, you are trying with a string term id instead of array, try this

$parent_terms = get_terms($taxonomyName, array('include' => array('67'), 'orderby' => 'slug', 'hide_empty' => false));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.