Skip to content

Instantly share code, notes, and snippets.

@jameskoster
Last active July 29, 2022 10:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jameskoster/5042430 to your computer and use it in GitHub Desktop.
Save jameskoster/5042430 to your computer and use it in GitHub Desktop.
WooCommerce - display category image on category archive
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="' . $cat->name . '" />';
}
}
}
@lesmimis
Copy link

Hello,

it worked very well on woocommerce before 2.1 update. There is no longer a file functions.php.
Do you know where I can paste the code now, with the latest version of woocommerce?

Thank you for your answer.
Lesmimis

@fredmat
Copy link

fredmat commented Apr 18, 2014

A good way to do is to create a woocommerce.php in your theme and put all functions related to your shop there.
After in your functions.php of your theme:

require(get_template_directory() . '/woocommerce.php');

Hope it helps

@morktron
Copy link

This works fine in a child themes function.php file. The issue is that the image is outside of the .term-description div. This is generated by wc-template-functions.php line 476. Is there any way to add the image inside the containing div?

Otherwise it will require a lot of extra CSS to position the image relative to the container responsively.

@tiknius
Copy link

tiknius commented Sep 21, 2015

Hi, how this code can be edited so parent category image is also displayed when viewing child categories? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment