Skip to content

Snippet: Display WooCommerce Category Image Above Products


add_action( 'wpex_hook_header_after', function() {

	if ( ! is_singular( 'product' ) || ! function_exists( 'wpex_get_first_term_id' ) || ! function_exists( 'wpex_get_term_thumbnail_id' ) ) {
		return;
	}

	// Get first category
	$term = wpex_get_first_term_id( get_the_ID(), 'product_cat' );

	// No terms, lets not do anything
	if ( ! $term ) {
		return;
	}

	// Get term thumbnail
	$term_thumbnail = wpex_get_term_thumbnail_id( $term );

	// Great we have a thumbnail, now lets show it
	if ( $term_thumbnail ) {
		echo '<div class="my-product-banners"><img src="' . esc_url( wp_get_attachment_url( $term_thumbnail ) ) . '" /></div>';
	}

} );
All PHP snippets should be added via child theme's functions.php file or via a plugin. We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top