A
A
anton99992020-04-27 17:35:38
WooCommerce
anton9999, 2020-04-27 17:35:38

How to set a specific size for product catalog thumbnails?

How to set a specific thumbnail size, which differs from the standard thumbnail sizes, for a specific category or subcategory of products to display them in the catalog.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anton9999, 2020-04-27
@anton9999

I found such code, but I don't understand how to change it so that it applies to specific product categories or subcategories. And where is the size of the thumbnail set.

add_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail_hook', 2 );
 
 function woocommerce_subcategory_thumbnail_hook( $category ) {
 $small_thumbnail_size   = apply_filters( 'woocommerce_subcategory_thumbnail', 'full' );
 $dimensions     = wc_get_image_size( $small_thumbnail_size );
 $thumbnail_id   = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
 
 if ( $thumbnail_id ) {
 $image        = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
 $image        = $image[0];
 $image_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $thumbnail_id, $small_thumbnail_size ) : false;
 $image_sizes  = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $thumbnail_id, $small_thumbnail_size ) : false;
 } else {
 $image        = wc_placeholder_img_src();
 $image_srcset = $image_sizes = false;
 }
 
 if ( $image ) {
 // Prevent esc_url from breaking spaces in urls for image embeds.
 // Ref: https://core.trac.wordpress.org/ticket/23605.
 $image = str_replace( ' ', '%20', $image );
 
 // Add responsive image markup if available.
 if ( $image_srcset && $image_sizes ) {
 echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" srcset="' . esc_attr( $image_srcset ) . '" sizes="' . esc_attr( $image_sizes ) . '" />';
 } else {
 echo '<img src="' . esc_url( $image ) . '" alt="' . esc_attr( $category->name ) . '" width="' . esc_attr( $dimensions['width'] ) . '" height="' . esc_attr( $dimensions['height'] ) . '" />';
 }
 }
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question