Mohammed Abrar Ahmed
Mohammed Abrar Ahmed

Reputation: 2490

Display all images that are assign to a product in woo commerce , wordpress

I have a running site designed in wordpress, I am trying to fetch all the images for a product that is defined in a woo commerce and display them one after the other i have tried doing the below but it didn't work out.

<?php while(the_post_thumbnail()) the_post_thumbnail(); ?>

Upvotes: 1

Views: 1082

Answers (1)

Dezefy
Dezefy

Reputation: 2096

If you want to get product gallery images anywhere. You can try this. It will fetch the images by id.

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

            }
    ?>

If you are displaying the images in single product page.

Then

<?php
    global $product;

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

        }
?>

Upvotes: 1

Related Questions