【问题标题】:How to show conditional image thumbnail size in Woocommerce如何在 Woocommerce 中显示条件图像缩略图大小
【发布时间】:2017-11-28 10:44:43
【问题描述】:

当我正在开发一个库存照片网站时,我想向我的访问者展示他的图片的大预览。为避免图像被盗,我对我的图像加了水印,但全尺寸除外。为了提供大预览,我创建了一个自定义缩略图大小并相应地更改了代码。请参阅线程here

有时上传的图片尺寸较小,例如 3072 像素(宽度或高度)。我想要的是在这种情况下有一个名为“大”的缩略图的后备,这是默认的 Wordpress 图像尺寸之一。 就像现在一样,它正在回落到全尺寸图像。当全尺寸图像小于 3072 像素时,似乎不会生成 3072 像素的缩略图。

我试图通过下面的代码来完成它,但无法正确完成。 怎么了?

$filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE ); 

$image_width       = $filemeta['width'];
$image_height      = $filemeta['height'];


if ($image_width < 3072 || $image_height < 3072){
$thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'large' );
} else {
$thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );
}

【问题讨论】:

    标签: php wordpress image woocommerce


    【解决方案1】:

    找到了。

    在 product_image.php 中更改了这一行

    $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );
    

    add_image_size( 'preview', $width = 3072, $height =3072, $crop = false ); 
    
    $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
    $filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE );
    
    if ($filemeta['width']>3071 || $filemeta['height']>3071){
        $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'preview' );
    }else{
        $thumbnail_size    = apply_filters( 'woocommerce_product_thumbnails_large_size', 'large' );
    }
    

    也许有更聪明的方法,但这对我有用。

    编辑 2018-06-26 一段时间后我选择了这个项目,并注意到由于 WooCommerce 更新到 3.3,它不再工作了。 WooCommerce 改变了处理产品图像的方式。对我来说,这意味着我回到了第一方。 WooCommerce 在灯箱中显示全尺寸图像,这不是我想要的。有人有线索吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-09
      • 2014-12-05
      • 1970-01-01
      • 2017-05-10
      • 2018-02-23
      • 1970-01-01
      • 2014-09-21
      • 2016-10-29
      相关资源
      最近更新 更多