Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Theming] Display category image on category archive
/**
* 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_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img src="' . $image . '" alt="' . $cat->name . '" />';
}
}
}
@Tommyhuls

This comment has been minimized.

Copy link

@Tommyhuls Tommyhuls commented Jun 15, 2018

Hi,

Is it possible to add a div id so that it can for example be displayed 100% width, and is it possible to add the category name as a header on top of the pic?

@braddalton

This comment has been minimized.

Copy link

@braddalton braddalton commented Sep 7, 2018

@Tommyhuls Try something like this

echo '<img class="woo-image" src="' . $image . '" alt="' . $cat->name . '" />';

@NVMDSN

This comment has been minimized.

Copy link

@NVMDSN NVMDSN commented Dec 12, 2018

Hi,
When I put this code, the image shows under title in product category page. Any idea how to show the image in the right side of the title?
Thanks.

@Domik83

This comment has been minimized.

Copy link

@Domik83 Domik83 commented Feb 19, 2020

Hi,
I have the opposite requirement: my template displays the categories images and I don't want them. How and in which file can I modify that?
Thanks.

@Machaddict

This comment has been minimized.

Copy link

@Machaddict Machaddict commented Nov 25, 2020

Hi, is possible to add a link to the image?
I need to have an image with external link in some specific category page for legal reason.

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.