代码之家  ›  专栏  ›  技术社区  ›  Cray

WooCommerce:从图库缩略图更改大小

  •  0
  • Cray  · 技术社区  · 6 年前

    我想将图库缩略图的缩略图大小更改为最大60x60px。不用修剪。

    我在WooCommerce文档中发现了以下钩子:

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

    60x60版本在服务器上。但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, );
    } );
    

    这些图像具有最大宽度和高度,不会被裁剪。 但是图库缩略图版本总是被裁剪。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Cray    6 年前

    我找到了一个解决方案,我猜是过滤器里的名字不对。

    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,
        );
    }
    

    不幸的是,gallery滑块导航栏使用的是150x150版本的图像。但这并不是这个问题的重点。

    推荐文章