如果仅将一张图像添加到产品WooCommerce中,如何删除产品缩略图 [英] How to remove product thumbnail if only one image is added to product WooCommerce

查看:244
本文介绍了如果仅将一张图像添加到产品WooCommerce中,如何删除产品缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很好,我想知道是否可以从单个产品页面上删除产品缩略图,因为产品只有一个图像(即仅产品图像).因此,当用户仅查看一个图像时,他们就不需要看缩略图,但是带有产品图片和产品库图片的产品,缩略图就会显示出来.

Good I was wondering if i could remove product thumbnail from single product page is product has only one image (i.e the product image only).. So that when user are viewing the product with only one image, they dont need to see the thumbnail but products with product image and Product gallery images, the thumbnail can show up.

有没有办法做到这一点?

Is there a way to achieve this?

我尝试了以下操作,但对我却没有用(尽管代码是要完全删除缩略图);

I have tried the below but didn't work for me (though the code is to remove the thumbnail entirely);

function remove_gallery_thumbnail_images() {
    if ( is_product() ) {
        remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
    }
}
add_action('loop_start', 'remove_gallery_thumbnail_images');

我该如何实现?如果产品只有一幅图像,则禁用缩略图;如果产品具有多幅图像,则显示缩略图.

How can i achieve this? disable the thumbnail if product has only one image but display thumbnails if product has multiple images.

非常欢迎任何帮助.

推荐答案

通常,当其中没有缩略图时,woocommerce不会显示画廊.

Normally woocommerce doesn't show the gallery when there is no thumbnails in it.

根据您的情况,您可以尝试使用以下内容:

In your case, you can try to use the following:

add_action( 'woocommerce_product_thumbnails', 'enable_gallery_for_multiple_thumbnails_only', 5 );
function enable_gallery_for_multiple_thumbnails_only() {
    global $product;

    if( ! is_a($product, 'WC_Product') ) {
        $product = wc_get_product( get_the_id() );
    }

    if( empty( $product->get_gallery_image_ids() ) ) {
        remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
    }
}

或者,如果图像作为缩略图包含在图库中,则可以在函数中替换:

Or if the image is included as a thumbnail in the gallery you can replace in the function:

if( empty( $product->get_gallery_image_ids() ) ) {

通过以下行:

if( sizeof( $product->get_gallery_image_ids() ) == 1 ) {

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

您还可以使用嵌入式CSS隐藏图库:

You can also hide the gallery with an inline CSS:

add_action( 'woocommerce_before_single_product_summary', 'enable_gallery_for_multiple_thumbnails_only', 5 );
function enable_gallery_for_multiple_thumbnails_only() {
    global $product;

    if( ! is_a($product, 'WC_Product') ) {
        $product = wc_get_product( get_the_id() );
    }

    if( empty( $product->get_gallery_image_ids() ) ) {
        echo '<style> ol.flex-control-thumbs { display:none; } </style>';
    }
}

或者,如果图像作为缩略图包含在图库中,则可以在函数中替换:

Or if the image is included as a thumbnail in the gallery you can replace in the function:

if( empty( $product->get_gallery_image_ids() ) ) {

通过以下行:

if( sizeof( $product->get_gallery_image_ids() ) == 1 ) {

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

所有这些都适用于没有进行相关自定义的主题.

All this works on themes that doesn't make related customizations.

这篇关于如果仅将一张图像添加到产品WooCommerce中,如何删除产品缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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