【问题标题】:WooCommerce add product image to gallery automatically (if gallery is empty)WooCommerce 自动将产品图片添加到图库(如果图库为空)
【发布时间】:2020-08-09 09:03:39
【问题描述】:

在 WooCommerce 中,产品可以有产品图片(单张图片)和产品图库(多张图片)。

当我为产品选择产品图片时,我希望它也自动添加到产品库中。

一件重要的事情:它应该检查产品图片是否已经在画廊中(所以画廊不会有重复的图片)。

有 php 函数吗?

(我怀疑these lines of code 可能有助于创建函数)

【问题讨论】:

    标签: php wordpress woocommerce product gallery


    【解决方案1】:

    我通过将产品图片添加到模板本身的图库数组中解决了这个问题。

    <?php
        global $product;
        $attachment_ids = $product->get_gallery_image_ids();
        $product_image_id = $product->get_image_id();
    
        // Add product image to gallery if its not there.
        // (This will prevent empty galleries, and also prevent duplicate images)
        if (!in_array($product_image_id, $attachment_ids)) {
            array_unshift($attachment_ids, $product_image_id);
        }
    
        foreach ($attachment_ids as $attachment_id) :
            $attachment_full_url = wp_get_attachment_image_src($attachment_id, 'full')[0];
        ?>
            <div class="swiper-slide"><a href="<?php echo $attachment_full_url; ?>"><?php echo wp_get_attachment_image($attachment_id, 'woocommerce_single'); ?></a></div>
        <?php
        endforeach;
    ?>
    

    但是,更好的解决方案是实际上将产品图片添加到图库本身,而不是使用模板代码来执行此操作。

    【讨论】:

    • 你能告诉我我应该把这段代码放在哪里吗?如何编辑产品模板?谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2023-02-08
    • 1970-01-01
    • 2020-10-23
    • 2013-06-21
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多