Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted
<?php
$args = array(
  'taxonomy'     => 'product-type',
  'hierarchical' => true,
  'title_li'     => '',
  'hide_empty'   => false
);
?>

<ul>
<?php wp_list_categories( $args ); ?>
</ul>

You can use the wp_list_categories function also for taxonomies. http://codex.wordpress.org/Template_Tags/wp_list_categories

share|improve this answer
    
nice, so simple, yet so many lost hairs :) tyvm –  Andy Jan 16 '12 at 20:18
add comment

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');
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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