0

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
  • 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.

  • 1
    Sorry but it's not clear… Your code seems to work. What is missing? What are you expecting that you doesn't have? – LoicTheAztec Feb 17 '21 at 18:26
  • @LoicTheAztec Sorry for not being clear in the first place. When I click a grand children category (eg Pants) I want to display its parent with its children and the one clicked just be highlighted. Clicking the Pants, I should get from the Clothes and down, and the Pants be highlighted. I believe an if statement is what I am missing. – Aleksander Feb 18 '21 at 10:06

0 Answers0