Display sibling categories in WooCommerce

On my WooCommerce website, I have the following Product categories:

• Clothing
  - Tee shirts
  - Jeans
  - Shirts

• Food
  - Bread
  - Cheese
  - Butter

On the category page for “Butter” I would like to show the name of the parent category and the sibling categories of the category “Butter”. This would look like this:

Read More
<h2>Food</h2>
<ul>
   <li>Bread</li>
   <li>Butter</li>
   <li>Cheese</li>
</ul>

Similarly, the category page for Jeans should show:

<h2>Clothing</h2>
<ul>
   <li>Jeans</li>
   <li>Shirts</li>
   <li>Tee Shirts</li>
</ul>

How would I achieve this?

Related posts

Leave a Reply

1 comment

  1. Just a first guess, but I think you could grab the category from the query vars, get the term object, and use the term’s parent ID to create a list of terms:

    $query_var = get_query_var( 'product_cat', '' );
    $term  = get_term_by( 'slug', $product_cat, 'product_cat' );
    if( $term->parent_id > 0 ){
        $args = array( 'taxonomy' => 'product_cat', 'child_of' => $term->parent_id );
        wp_list_categories( $args );
    }
    

    if wp_list_categories() doesn’t quite output what you’d want, you can always use get_terms() and run your own foreach() loop.