Created
February 18, 2019 14:43
-
-
Save NicBeltramelli/4686810981230a3810d724b06cafaf77 to your computer and use it in GitHub Desktop.
WooCommerce display product category image on category archive
This file contains 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
<?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