如何在Woocommerce中显示条件图像缩略图大小 [英] How to show conditional image thumbnail size in Woocommerce

查看:711
本文介绍了如何在Woocommerce中显示条件图像缩略图大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As I'm developing a stock photo site I want to show my visitors a large preview of he image. To avoid image theft I have watermarked my images except for the full size. To serve a large preview I created a custom thumbnail size and change the code accordingly. See the thread here.

案例是,有时上传的图像尺寸较小,为3072像素(宽度或高度)。我想要的是,在这种情况下,缩略图称为大,这是默认的Wordpress图像大小之一。
因为它现在正在回落到全尺寸图像。当全尺寸图像小于3072px时,似乎没有生成尺寸为3072像素的缩略图。

The case is that sometimes an image is uploaded with smaller dimensions as the 3072 px (width or height). What I want is that there is a fallback in this case to the thumbnail called 'large' which is one of the default Wordpress image sizes. As it is right now it is falling back to the full size image. It appears that no thumbnail of size 3072 px is generated when the full size image is smaller then 3072px.

我试图通过下面的代码完成它但是无法做对。
怎么了?

I have tried to get it done by the piece of code below but can't get it right. What's wrong?

$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' );
}


推荐答案

找到它。

更改了product_image.php中的这一行

Changed this line in product_image.php

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

in

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

也许有一种更聪明的方式,但这对我有用。

Maybe there is a smarter way but this works for me.

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

EDIT 2018-06-26 I picked this project up after a while and noticed that it is not working anymore due to the WooCommerce update to 3.3. WooCommerce has changes the way it handles product images. For me it means that I'm back at square one. WooCommerce displays the full-size image in the light box and that is not what I want. Does someone have a clue?

这篇关于如何在Woocommerce中显示条件图像缩略图大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆