Display on Woocommerce sidebar children of parent category

Viewed 40

I have a WordPress sidebar that I want to display the children of the parent category. On the grand child categorie it should display the current categories.

My structure is as follows:

All categories -> Parent categories -> Child categories -> Grand Child categories

Here is a similiar Problem :

Only display Woo Commerce sub-categories based on selected Parent Category in a wordpress widget sidebar

but it doesnt show all categories and grand child categories.

This is the code I'm using:

add_shortcode('child_category_list', 'get_child_category_list');

function get_child_category_list(){ ob_start();

// Only on product parent category pages
if( is_product_category() ) {
    $parent = get_queried_object();

    $categories = get_term_children( $parent->term_id, 'product_cat' ); 
    if ( $categories && ! is_wp_error( $categories ) ) : 

        foreach($categories as $category) :
                    $term = get_term( $category, 'product_cat' );
                    echo '<li>';
                    echo '<a href="'.get_term_link($term).'" >';
                    echo $term->name;
                    echo '</a>';
                    echo '</li>';
        endforeach; 
        echo        '</ul>';
        echo    '<li>';
        echo '</ul>';

    endif;
}
return ob_get_clean();

}

Thanks!

Best regards

0 Answers
Related