Custom Taxonomy Tree view

I have been trying google for this, but not so easy to search for. I have a custom hierarchical taxonomy, something along the lines of:

Chainsaws
- Electric
- Petrol
- Other
Grasscutters
- Electric
- Petrol
- Other

What I need to do is create an index page, retaining the hierarchical structure.

Read More

The closest I have come to doing it is with:

$products = get_terms('product-type');
foreach ($products as $product) {
      $out .= $product->name;
}

But this just shows the ones in use, and loses it hierarchy 🙁

Any insights are very welcome.

Thanks in advance

Andy

Related posts

Leave a Reply

2 comments

  1. Here is something I whipped up:

    <?php
    //Walker function
    function custom_taxonomy_walker($taxonomy, $parent = 0)
    {
        $terms = get_terms($taxonomy, array('parent' => $parent, 'hide_empty' => false));
        //If there are terms, start displaying
        if(count($terms) > 0)
        {
            //Displaying as a list
            $out = "<ul>";
            //Cycle though the terms
            foreach ($terms as $term)
            {
                //Secret sauce.  Function calls itself to display child elements, if any
                $out .="<li>" . $term->name . custom_taxonomy_walker($taxonomy, $term->term_id) . "</li>"; 
            }
            $out .= "</ul>";    
            return $out;
        }
        return;
    }
    
    //Example
    echo custom_taxonomy_walker('category');
    ?>