'product’, 'posts_per_page’ => 12, // You can adjust the number of products displayed here ); if (!empty($query_vars[’product_cat’])) { // Decode the category slug to ensure it’s properly formatted $product_cat_slug = urldecode($query_vars[’product_cat’]); // Replace any encoded slashes with actual slashes if necessary $product_cat_slug = str_replace(’%2F’, '/’, $product_cat_slug); //echo $product_cat_slug; $slug_parts = explode(’/’, $product_cat_slug); // Split slug by '/’ $last_slug = end($slug_parts); // Get the last element $args[’tax_query’] = array( array( 'taxonomy’ => 'product_cat’, 'field’ => 'slug’, 'terms’ => $last_slug, 'include_children’ => true, // This is crucial for including products in child categories ), ); } } else { $args = array( 'post_type’ => 'product’, 'posts_per_page’ => 12, // Adjust the number of products displayed here ); } $products_query = new WP_Query($args); if ($products_query->have_posts()) { while ($products_query->have_posts()) { $products_query->the_post(); global $product; ?>