I am trying to create a custom widget for an eshop with many top categories each having its own child categories (~8) each having quite a few grand child categories (~9-15). e.g.
- Man
- ...
- Woman
- Clothes
- Hats
- Shirts
- Pants
- Skirts
- Shoes
- Accessories
- Clothes
- Child
- ...
When someone clicks Woman, in the sidebar of the page, I want to show only the child categories (clothes, shoes, accessories). When someone clicks either Clothes or one of its child categories (hats, shirts, ...), I want to show the only the part from Clothes and below highlighting the current category the user is (eg Pants). My problem is with the output part of the widget where I am totally at loss on how to achieve the above scenario (if it is achievable). My code so far.
// This is where you run the code and display the output
$queried_object = get_queried_object();
if ( is_product_category() && is_a($queried_object, 'WP_Term') ) {
$taxonomy = $queried_object->taxonomy;
echo '<h4><a href="' . get_term_link( $queried_object ) . '">' . $queried_object->name . '</a></h4>';
$children_terms = get_terms(['taxonomy' => $taxonomy, 'parent' => $queried_object->term_id]);
if ( ! empty($children_terms) ) {
echo '<ul class="'.$queried_object->slug.' child-terms">';
// Loop through children terms
foreach ( $children_terms as $term) {
echo '<li class="filters"><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
}
}
Any help greatly appreciated.