I'm building a product catalog site and I created a product
custom post type and a related product_type
taxonomy. I added some products and created the following product_type
terms:
- Books
- Classics
- Fantasy
- Mystery
- Toys
- Puzzles
- Dolls
- Cars
When a user visits the catalog I want to show him a product_type
terms navigation by level. That is, when he enters the main catalog page I only want him to see the first level of terms: Books, Toys and a special link named All that when clicked shows all products.
When the user clicks one of the root terms, for example, Books, I want to show its child terms in addition to the root terms, that is: Classics, Fantasy, Mystery and a special link named All that when clicked shows all products under the Books term.
Likewise, when the user clicks the Toys root term, I want to show the its child terms in addition to the root terms, that is: Puzzles, Dolls, Cars and a special link named All that when clicked shows all products under the Toys term.
I'm using the following recursive function to get all the terms hierarchically:
function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
$taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
$terms = get_terms( $taxonomy, array( 'parent' => $parent ) );
$children = array();
foreach ( $terms as $term ){
$term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );
$children[ $term->term_id ] = $term;
}
return $children;
}
But I'm hitting a wall trying to handle the All links in all of this structure because they are not actual terms per se. Any ideas would be appreciated.