Woocommerce 通过产品 ID 获取图片库

Woocommerce get image galleries by product id

如何以编程方式显示产品库中的图片?我看到显示特色图片的选项;但不显示产品 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;  

?>

我已经测试过了并且有效

<?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_image_ids() 获取图库附件 ID,因为 get_gallery_attachment_ids() 自 WooCommerce 3.0.0 后已弃用

澄清一下,上面的答案除了缩略图(主)图像之外的所有答案。

要添加缩略图,请使用:

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