Skip to content

Instantly share code, notes, and snippets.

@NicBeltramelli
Created February 18, 2019 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicBeltramelli/4686810981230a3810d724b06cafaf77 to your computer and use it in GitHub Desktop.
Save NicBeltramelli/4686810981230a3810d724b06cafaf77 to your computer and use it in GitHub Desktop.
WooCommerce display product category image on category archive
<?php
// Do NOT include the opening php tag.
/**
* Display category image on WooCommerce category archive
*
* @author Nic Beltramelli
*/
add_action(
'woocommerce_archive_description', function () {
if ( class_exists( 'WooCommerce' ) ) :
if ( is_product_category() ) :
global $wp_query;
$cat = $wp_query->get_queried_object();
$cat_name = $cat->name;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$size = 'thumbnail';
$image = wp_get_attachment_image_src( $thumbnail_id, $size );
if ( $image ) :
echo '<img src="' . esc_url( $image[0] ) . '" class="archive-image" alt="' . esc_attr( $cat_name ) . '" />';
endif;
endif;
endif;
}, 2
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment