Woocommerce show subcategory thumbnail on category page

On the category page there are different products in the same category which are assigned to a subcategory. Below a single product I want to show the thumbnail of the subcategory which the product is assigned to. I’ve used this code to show the thumbnail:

    <?php 
        global $wp_query;
        $cat = $wp_query->get_queried_object();
        $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
        $image = wp_get_attachment_url( $thumbnail_id );
        echo wp_get_attachment_image( $thumbnail_id ); 
    ?>  

The result is that is shows the thumbnail of the parent category (the category where all these products are in) instead of the subcategory thumbnail.

Read More

See here: http://www.solar-discounter.nl/product-categorie/panelen/

I hope someone has the answer for me, thanks in advance.

Related posts

Leave a Reply

2 comments

  1. Solved it!

    I replaced the code with the following:

    <?php
        $terms = get_the_terms( $post->ID, 'product_cat' );
        foreach ( $terms as $term ){
            $category_name = $term->name;
            $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
            $image = wp_get_attachment_url($category_thumbnail);
            echo '<img src="'.$image.'">';
        }
    ?>
    

    Thanks to Filespit @ StackExchange

    (https://wordpress.stackexchange.com/questions/87950/show-category-images-on-single-product-page-and-product-overview-page)

  2. The WooCommerce reference say to do this in themes/yourtheme/functions.php

        add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
    function woocommerce_category_image() {
        if ( is_product_category() ){
            global $wp_query;
            $cat = $wp_query->get_queried_object();
            $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
            $image = wp_get_attachment_url( $thumbnail_id );
            if ( $image ) {
                echo '<img src="' . $image . '" alt="" />';
            }
        }
    }