【问题标题】:Woocommerce get image galleries by product idWoocommerce 按产品 ID 获取图片库
【发布时间】:2016-09-13 11:15:01
【问题描述】:

如何以编程方式显示产品库中的图片?我看到了显示特色图片的选项;但不显示产品 ID 的所有图像。

可以这样做吗?

我尝试了这个,但无法通过 Woocommerce 中的 id 从产品库中获取图像。

<?php

 $gallery = get_post_gallery_images(724);


    $image_list = '<ul id="cfImageGallery">';                       
    foreach( $gallery as $image ) {// Loop through each image in each gallery
        $image_list .= '<li><img src=" ' . str_replace('-150x150','',$image) . ' " /></li>';
    }
    $image_list .= '</ul>';                     
    echo $image_list;  

?>

【问题讨论】:

    标签: wordpress woocommerce wordpress-theming


    【解决方案1】:

    我已经测试过了,它可以工作

    <?php
        $product_id = '14';
        $product = new WC_product($product_id);
        $attachment_ids = $product->get_gallery_image_ids();
    
        foreach( $attachment_ids as $attachment_id ) 
            {
              // Display the image URL
              echo $Original_image_url = wp_get_attachment_url( $attachment_id );
    
              // Display Image instead of URL
              echo wp_get_attachment_image($attachment_id, 'full');
    
            }
    ?>
    

    【讨论】:

    • get_gallery_attachment_ids() 函数自 woocommerce 3.0.0 起已弃用,您可以使用 get_gallery_image_ids()。
    【解决方案2】:

    澄清一下,上述答案将得到除缩略图(主)图像之外的所有内容。

    要添加缩略图,请使用:

    $product_id = '652';
    $product = new WC_product($product_id);
    $attachment_ids = $product->get_gallery_image_ids();
    

    //这会将缩略图id添加为数组的第一个元素

    array_unshift($attachment_ids, get_post_thumbnail_id($product_id));
    
    print_r($attachment_ids);
    

    【讨论】:

      【解决方案3】:

      使用 get_gallery_image_ids() 获取图库附件 ID,因为自 WooCommerce 3.0.0 以来不推荐使用 get_gallery_attachment_ids()

      【讨论】:

        【解决方案4】:
        <div class="row">
        <?php
        
            $attachment_ids = $product->get_gallery_attachment_ids();
        
            foreach( $attachment_ids as $attachment_id ) {
                $image_link =wp_get_attachment_url( $attachment_id );
                //Get image show by tag <img> 
                echo '<img class="thumb" src="' . $image_link . '">';
            }
        ?>
        </div>
        
        //Get all image by tag <img>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-02-26
          • 2019-03-03
          • 2016-03-04
          • 2014-07-04
          • 2016-01-09
          相关资源
          最近更新 更多