0

To the lovely people of StackExchange,

I've created the following code to list WooCommerce categories on category archive pages, and it's mostly working as I'd like.

However, I'd like to modify it so that sibling categories are shown on the deepest category. I was just wondering if any of you might have any thoughts on the best way to go about this?

$parentid = get_queried_object_id();
                     
                    $args = array(
                        
                        'parent' => $parentid,
                        'hide_empty' => true
                        
                    );
                     
                    $terms = get_terms( 'product_cat', $args );
                    
                    $number = sizeof ($terms);
                     
                    if ( $terms ) {
                        
                    echo '<div class="product-cat-list">';
                    
                        if ( $number > 12 ) {
                            
                        echo '<div class="product-cat-list__outer">';
                            
                        }
                        
                        echo '<h3>Find your ideal item</h3>';
                        
                        echo '<ul class="product-cats">';
                     
                        foreach ( $terms as $term ) {
                            
                            echo '<li class="category">';                 
                                          
                                echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
                                    echo $term->name;
                                echo '</a>';
                             
                            echo '</li>';
                            
                        }
                                             
                        echo '</ul>';
                        
                        if ( $number > 12 ) {
                            
                        echo '</div>';
                                
                        echo '<div class="product-cat-list__expander">';
                        
                            echo '<a href="#" class="read-more categories-expander">Show All</a>';
                        
                        echo '</div>';
                            
                        }
                        
                    echo '</div>';
                        
                    }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.