I am able to get a list of its subcategories in a category archive page using below code in archive-product.php
but I want to display the list of the products currently assigned to each subcategory.
<?php
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid
);
$categories = get_terms(
'product_cat', $args
);
if ( $categories ) {
foreach ( $categories as $category ) {
echo $category->name;
}
}
?>
Example:
Subcategory 1
- Product 1 of Subcategory 1
- Product 2 of Subcategory 1
Subcategory 2
- Product 1 of Subcategory 2
- Product 2 of Subcategory 2
I am still learning WordPress so any help will be highly appreciated.
I am able to get a list of its subcategories in a category archive page using below code in archive-product.php
but I want to display the list of the products currently assigned to each subcategory.
<?php
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid
);
$categories = get_terms(
'product_cat', $args
);
if ( $categories ) {
foreach ( $categories as $category ) {
echo $category->name;
}
}
?>
Example:
Subcategory 1
- Product 1 of Subcategory 1
- Product 2 of Subcategory 1
Subcategory 2
- Product 1 of Subcategory 2
- Product 2 of Subcategory 2
I am still learning WordPress so any help will be highly appreciated.
Share Improve this question edited Jul 29, 2018 at 13:44 Krzysiek Dróżdż 25.5k9 gold badges53 silver badges74 bronze badges asked Jul 29, 2018 at 10:35 Kevin SKevin S 133 bronze badges2 Answers
Reset to default 1<?php
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid
);
$categories = get_terms(
'product_cat', $args
);
if ( $categories ) :
foreach ( $categories as $category ) :
echo esc_html($category->name);
$products = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category->slug,
),
)
) );
if ( $products->have_posts() ) :
?>
<ul>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php
wp_reset_postdata();
endif;
endforeach;
endif;
?>
here is my code for this. You can find it here: [Archive category clustered by child categories][1]
It also displays number of posts in child category and link to child category.
$term = get_queried_object(); // Get current archive page
$children = get_terms($term->taxonomy, array(
'parent' => $term->term_id,
'hide_empty' => false
));
if ($children) {
foreach ($children as $subcat) {
echo '<h2><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . // Display child taxonomy name
$subcat->name . ' (' . $subcat->count . ')' . '</a></h2>' . $subcat->description; // Display child taxonomy item count
$kategorie = $subcat->slug; // Set child category slug for each query of products
$args = array(
'post_type' => 'product',
'product_cat' => $kategorie,
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post(); ?>
<h4><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); //Display product title ?></a></h4>
<?php $product = wc_get_product(get_the_ID()); // get the WC_Product Object ?>
<p><?php echo $product->get_price_html(); // Display product Price ?></p>
<?php endwhile; ?><?php
} else {
echo __('No products found');
}
wp_reset_postdata(); // Reset Query
?>
<!--/.products-->
<?php
}
}
?>
[1]: https://toolset.wiki/article/woocommerce-category-clustered-by-child-taxonomies-in-wordpress/