Woocommerce Gets Image Galleries by Product ID

How to programmatically display images from the product gallery? I see the ability to display selected images; but do not show all images for the product identifier.

Can this be done?

I am trying to do this, but could not get the images from the product gallery by id in Woocommerce.

<?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;  

?>
+4
source share
3 answers

I already tested this and it works

<?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');

        }
?>
+12
source

Use get_gallery_image_ids () to get the Gallary Attechment bcoz get_gallery_attachment_ids () identifiers that have been deprecated since woocommerce 3.0.0 was created

+1
source

, , () .

To add a sketch, use this:

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

// This adds the thumbnail id as the first element of the array

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

print_r($attachment_ids);
0
source

All Articles