2016-05-17 53 views
4

如何以编程方式显示产品图库中的图像?我看到了显示精选图像的选项;但不能显示产品ID的所有图像。Woocommerce通过产品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; 

?> 

回答

9

我已经测试了这一点,它的工作原理

<?php 
    $product_id = '14'; 
    $product = new WC_product($product_id); 
    $attachment_ids = $product->get_gallery_attachment_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'); 

     } 
?> 
+0

谢谢你,这是适用于我:) –

+0

get_gallery_attachment_ids()函数已弃用,因为woocommerce 3.0.0可以使用get_gallery_image_ids()。 –

1

使用得到GALLARY Attechment ID的get_gallery_image_ids()bcoz get_gallery_attachment_ids()因为woocommerce弃用3.0.0

相关问题