简体   繁体   English

如果图库存在,则从 WooCommerce 图库中删除特色图片

[英]Remove featured image from the WooCommerce gallery if gallery exist

I'm trying to remove the featured image from woocommerce gallery, but only if gallery exist.我正在尝试从 woocommerce 画廊中删除特色图片,但前提是画廊存在。

I have tried " Remove featured image from the WooCommerce gallery " answer code to remove the image, but also removes from other products too that don't have gallery, leaving the product without the main image.我试过从 WooCommerce 画廊中删除特色图片答案代码来删除图片,但也从其他没有图库的产品中删除,使产品没有主图片。

I know that Wordpress has a function get_post_gallery() to check this, but what about woocommerce?我知道 Wordpress 有一个函数get_post_gallery()来检查这个,但是 woocommerce 呢?

So, my main question is: How to check if a product gallery exist to implement this code?所以,我的主要问题是:如何检查是否存在产品库来实现此代码?

Just add follows code snippet -只需添加以下代码片段 -

function woocommerce_single_product_image_thumbnail_html( $html, $attachment_id ) {
    global $product;
    $attachment_ids = $product->get_gallery_image_ids();

    return ( $attachment_ids && $product->get_image_id() == $attachment_id ) ? '' : $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'woocommerce_single_product_image_thumbnail_html', 99, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM