Last active
July 29, 2022 10:01
-
-
Save jameskoster/5042430 to your computer and use it in GitHub Desktop.
WooCommerce - display category image on category archive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . '" />'; | |
} | |
} | |
} |
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
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.
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
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