Woocommerce nested main categories to show subcategories
-
I am a rather rookie in php but succeeded thanks to some tutorials so that my Woocommerce index catalogue page is showing nested categories. The structure is as follows
MAIN 1
– subcategory
– subcategory
MAIN 2
– subcategory
– subcategoryWhen clicked on the MAIN category it displays nested sub-categories which is all good. Although It is also needed to display the subcategories in the index page (as a All products page – currently displaying two MAIN categories with products which is a mess). At the moment all products are displaying randomly under the MAIN categories.
I haven’t had any luck in achieving this, my content-product_cat.php is as follows:
‘
<?php
if ( ! defined( ‘ABSPATH’ ) ) exit; // Exit if accessed directlyglobal $woocommerce_loop;
// Store loop count we’re currently on
if ( empty( $woocommerce_loop[‘loop’] ) )
$woocommerce_loop[‘loop’] = 0;// Store column count for displaying the grid
if ( empty( $woocommerce_loop[‘columns’] ) )
$woocommerce_loop[‘columns’] = apply_filters( ‘loop_shop_columns’, 1 );// Increase loop count
$woocommerce_loop[‘loop’]++;
?><?php do_action( ‘woocommerce_before_subcategory’, $category ); ?>
<div style=”float: left;
width: 100%;” id=”peakategooria”>
<?php echo $category->name; ?>
<?php if ( $category->count > 0 ) : ?>
</div>
<li class=”product-category product<?php
if ( ( $woocommerce_loop[‘loop’] – 1 ) % $woocommerce_loop[‘columns’] == 0 || $woocommerce_loop[‘columns’] == 1)
echo ‘ first’;
if ( $woocommerce_loop[‘loop’] % $woocommerce_loop[‘columns’] == 0 )
echo ‘ last’;
?>”><h3>
<?php
$value2 = $category->slug;
$args = array(
‘post_type’ => ‘product’,
‘posts_per_page’ => 100,
‘product_cat’ => $value2,
‘orderby’ => ‘name’,
‘order’ => ‘ASC’,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php woocommerce_get_template_part( ‘content’, ‘product’ ); ?>
<?php endwhile; ?>
<?php endif; ?>
</h3><?php
/**
* woocommerce_after_subcategory_title hook
*/
do_action( ‘woocommerce_after_subcategory_title’, $category );
?><?php do_action( ‘woocommerce_after_subcategory’, $category ); ?>
‘
Any help or guidance would be appreciated dearly!O.
- The topic ‘Woocommerce nested main categories to show subcategories’ is closed to new replies.