add_shortcode('child_category_list', 'get_child_category_list');
function get_child_category_list(){
ob_start();
// Only on product parent category pages
if( is_product_category() ) {
$parent = get_queried_object();
$categories = get_term_children( $parent->term_id, 'product_cat' );
if ( $categories && ! is_wp_error( $categories ) ) :
echo '<ul>';
echo '<li>';
echo '<a href="'.get_term_link ($parent->term_id, 'product_cat').'">'.$parent->name.'</a>';
echo '<ul>';
foreach($categories as $category) :
$term = get_term( $category, 'product_cat' );
echo '<li>';
echo '<a href="'.get_term_link($term).'" >';
echo $term->name;
echo '</a>';
echo '</li>';
endforeach;
echo '</ul>';
echo '<li>';
echo '</ul>';
endif;
}
return ob_get_clean();
}
<div class="cat-container">
<?php
$categories = get_terms(
'product_cat',
array(
'orderby' => 'menu_order',
'order' => 'ask',
'hierarchical' => true,
'hide_empty' => 0,
'parent' => 0
)
);
//print_r($categories);
foreach($categories as $cat){
$cat_thumb_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_image_src( $cat_thumb_id, 'thumbnail-size' )[0];
?>
<div class="cat-sub">
<?php $temp_cat = get_terms(
'product_cat',
array(
'orderby' => 'menu_order',
'order' => 'ask',
'hierarchical' => true,
'hide_empty' => 0,
'parent' => $cat->term_id
)
);
$class='';
if($temp_cat) {$class='has_child';} else {$class='no_child';} ?>
<div class="parent-item-wrap d-flex" id="cat-parent-item-<?php echo $cat->term_id; ?>">
<div class="cat-parent-item col-xl-3 col-md-4 col-sm-6 col-12 product-category">
<div class="subcategory-inner">
<a href="<?php echo get_term_link( $cat->term_id,'product_cat' )?>" target='_blank'>
<img src="<?php echo $cat_thumb_url; ?>" alt="" />
<h2 class="woocommerce-loop-category__title"><?php echo $cat->name?></h2>
</a>
</div>
</div>
</div>
<?php
if($temp_cat){?>
<div class="child-items d-flex flex-wrap align-items-stretch">
<?php foreach($temp_cat as $temp){
$cat_thumb_id = get_woocommerce_term_meta( $temp->term_id, 'thumbnail_id', true );
$cat_thumb_url = wp_get_attachment_image_src( $cat_thumb_id, 'thumbnail-size' )[0];
?>
<div class="cat-item col-xl-3 col-md-4 col-sm-6 col-12 product-category" id="cat-item-<?php echo $temp->term_id; ?>">
<div class="subcategory-inner">
<a href="<?php echo get_term_link( $temp->term_id,'product_cat' )?>" target='_blank'>
<img src="<?php echo $cat_thumb_url; ?>" alt="" />
<h2 class="woocommerce-loop-category__title"><?php echo $temp->name; ?></h2>
</a>
</div>
</div>
<?php }?>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
function get_child_category_list( $parent_id, $level = 0 ) {
$args = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $parent_id,
'taxonomy' => 'product_cat'
);
$subcategories = get_categories( $args );
if ( $subcategories ) {
$output = '<ul class="subcategories level-' . $level . '">';
foreach ( $subcategories as $category ) {
$output .= '<li>';
$output .= '<a href="' . get_term_link( $category ) . '">' . $category->name . '</a>';
$output .= get_child_category_list( $category->term_id, $level + 1 );
$output .= '</li>';
}
$output .= '</ul>';
return $output;
} else {
return '';
}
}
if ( is_product_category() ) {
$parent_id = get_queried_object_id();
echo get_child_category_list( $parent_id );
}