L
L
LastGeneral2021-08-07 15:48:48
WordPress
LastGeneral, 2021-08-07 15:48:48

How to display subcategories under category name in woocommerce?

How to display subcategories with images under the category name?
Like this:
610e80fa7976a246987404.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LastGeneral, 2021-08-07
@LastGeneral

<?php
    	$parentid = get_queried_object_id();
    
    	$args = array(
    			'parent' => $parentid,
    			'hide_empty' => false
    	);
    
    	$terms = get_terms( 'product_cat', $args );
    
    	if ( $terms ) {  
    			echo '<div class="subcat-product"><ul class="subcat-product__content">';
    					foreach ( $terms as $term ) {
    					    $thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
    							echo '<li class="subcat-product__item">';
    																	
    									//woocommerce_subcategory_thumbnail( $term );
    									echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . ' catalog-list-block__title">';
    											echo '<img style="background-image: url('.  wp_get_attachment_url( $thumbnail_id ) .')!important;background-size: 100%;background-repeat: no-repeat;background-size: cover; width: 100px; height: 100px;" />';
    											echo $term->name;
    									echo '</a>';
    									
    									
    							echo '</li>';                                                                                                    
    			}
    			echo '</ul></div>';
    	}
    ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question