T.Doe
T.Doe

Reputation: 2035

Woocommerce: Add the category thumbnail image to my sidebar in product pages

I'm using WooCommerce widgets to add the category list for my products in the sidebar of my website. Currently the categories are listed as text items. I would like to display the thumbnail image for each category (beside the text).

I have used code below, but it shows the category thumbnail in the centre of the page where the products are listed:

add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
function woocommerce_category_image() {
        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="" />';
        }
} 

I would like this thumbnails in the sidebar beside the text items.

What am I missing to achieve this?

Upvotes: 3

Views: 5087

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253794

You have picked this code snippet at wooThemes: WC Display category image on category archive

You are using woocommerce_archive_description that is displaying this category image on archive-product.php template (line 48).

Sorry but this will not work for a widget especially using this hook.

Now you have 3 options:

  • Creating your own widget and coding it with php (the hardest).
  • Using a free plugin made for that: WooCommerce - Category widget (the easiest)
  • Or in between, picking some code and ideas in the plugin, to make your own…

Upvotes: 1

Related Questions