WooCommerce: 更改商品画廊缩略图大小

3

我希望将图库缩略图的尺寸更改为最大60x60像素,而不需要对它们进行裁剪。

我在WooCommerce文档中找到了以下挂钩:

add_filter( 'woocommerce_gallery_thumbnail_size', function( $size ) {
    return array('width' => 60, 'height' => 60, 'crop' => 0, );
} );

但是似乎裁剪参数没有效果?WordPress也忽略了这个大小,并在图库导航中始终显示来自WordPress自身的150x150像素版本。即使使用插件重新生成缩略图大小后仍然如此。 60x60版本位于服务器上。但它没有被WooCommerce使用,而且还被裁剪。 我也使用这段代码将WooCommerce支持添加到主题中:

function mytheme_add_woocommerce_support() {
    add_theme_support( 'woocommerce', array(
        'thumbnail_image_width'         => 240,
        'single_image_width'            => 450,
        'gallery_thumbnail_image_width' => 60,
    ) );
    add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', 'mytheme_add_woocommerce_support' );

当我删除它时,WooCommerce缩略图大小完全被忽略。

我做错了什么吗? 对于其他像这样的图像大小,它是有效的:

add_filter( 'woocommerce_get_image_size_thumbnail', function( $size ) {
    return array('width' => 240, 'height' => 240, 'crop' => 0, );
} );

add_filter( 'woocommerce_get_image_size_single', function( $size ) {
    return array('width' => 450, 'height' => 450, 'crop' => 0, );
} );

这些图片有最大的宽度和高度,没有被裁剪。 但是相册缩略图版本总是被裁剪。


啊,我从未使用过那个东西。但它也没有效果吗?我使用最后一个设置来不裁剪图像。但是在重新生成后它们仍然被裁剪了。 - Cray
1个回答

3

我找到了一个解决方案,我猜测是过滤器中使用的名称不正确。

我使用的名称 (woocommerce_gallery_thumbnail_size) 是从文档复制的,但他似乎是正确的版本: woocommerce_get_image_size_gallery_thumbnail

来自这里的提示: https://theme.co/forum/t/woocommerce-product-gallery-thumbnails-cropped/47367/2

这是他们的代码:

// change woocommerce thumbnail image size
add_filter( 'woocommerce_get_image_size_gallery_thumbnail', 'override_woocommerce_image_size_gallery_thumbnail' );
function override_woocommerce_image_size_gallery_thumbnail( $size ) {
    // Gallery thumbnails: proportional, max width 200px
    return array(
        'width'  => 60,
        'height' => 60,
        'crop'   => 0,
    );
}

遗憾的是,画廊滑块导航仍在使用150x150版本的图像。但这不是问题的重点。


1
150x150 可能是 wp 默认的“缩略图”大小。 - Alex T

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接