Displays the child category of WordPress's parent category with woocommerce

Viewed 161

f you go to any category or sub-category, all the child categories of the parent category of that category will show. I found some code in the stackoverflow. Works fine. But there is a problem here. I want to display category thumbnails with each category. How do I add it to this code? Or it can be done in any other method. Any ideas?

  if( is_product_category() ){
    $queried_object = get_queried_object();
    $child_terms    = get_term_children ( 
        $queried_object->term_id, 'product_cat' );
    $based_term     = (is_wp_error($child_terms) || empty($child_terms)) ? get_term ( 
        $queried_object->parent, 'product_cat' ) : $queried_object;

    printf( '<h2 class="shop__sidebar-heading">
        <a href="%s?so64231449=true">%s</a>
    </h2>', esc_url(get_term_link($based_term->term_id)), $based_term->name );

    $display_terms = get_terms( 'product_cat', array(
        'orderby'       => 'name', 
        'order'         => 'ASC',
        'hide_empty'    => false,
        'parent'        => $based_term->term_id,
    ) );

    if( !empty($display_terms) && !is_wp_error($display_terms) ){
        ;echo '';
      
            foreach( $display_terms as $display_term ){
                printf(
                    /* '<h6%s><a href="%s">%s <span class="count">%s</span></a></h6>', */
                    '<div class="col-md-3"><div class="content-inner"><h6%s><a href="%s">%s</a></h6></div></div>',
                    ($display_term->term_id == $queried_object->term_id) ? ' class="active"' : '',
                    esc_url(get_term_link($display_term->term_id)),
                    $display_term->name,
                   /*  number_format($display_term->count) */
                );
            }
        echo '';
       
    }
}
1 Answers

Use WC get_woocommerce_term_meta to get term_meta you can find thumbnail id in using meta key thumbnail_id. and use WP wp_get_attachment_url to get image url. check the below code.

if( is_product_category() ){

    $queried_object = get_queried_object();
    $child_terms    = get_term_children ( $queried_object->term_id, 'product_cat' );
    $based_term     = (is_wp_error($child_terms) || empty($child_terms)) ? get_term ( $queried_object->parent, 'product_cat' ) : $queried_object;

    printf( '<h2 class="shop__sidebar-heading"><a href="%s?so64231449=true">%s</a></h2>', esc_url(get_term_link($based_term->term_id)), $based_term->name );

    $display_terms = get_terms( 'product_cat', array(
        'orderby'       => 'name', 
        'order'         => 'ASC',
        'hide_empty'    => false,
        'parent'        => $based_term->term_id,
    ) );

    if( !empty( $display_terms ) && !is_wp_error( $display_terms ) ){

        echo '';
      
            foreach( $display_terms as $display_term ){

                $thumbnail_id = get_term_meta ($display_term->term_id, 'thumbnail_id', true);
                $image        = wp_get_attachment_url( $thumbnail_id );
                
                printf(
                    '<div class="col-md-3">
                        <div class="content-inner">
                            <h6%s><a href="%s">%s</a></h6>
                            <img src="%s" alt="" />
                        </div>
                    </div>',
                    ($display_term->term_id == $queried_object->term_id) ? ' class="active"' : '',
                    esc_url(get_term_link($display_term->term_id)),
                    $display_term->name,
                    $image,
                );
            }

        echo '';
       
    }
}
Related