Retrieve WooCommerce Product Galleries Dynamically by ID

Use this foreach loop (inside of a query) to grab Product galleries from WooCommerce.

<?php
  global $product;
 $attachment_ids = $product->get_gallery_attachment_ids();

foreach( $attachment_ids as $attachment_id ) 
{
  echo $image_link = wp_get_attachment_url( $attachment_id );
}
?>

SourcedĀ here.

This snippet will also help you to convert the URL into an ID:

$attachment_id = attachment_url_to_postid( $image_url );
echo $attachment_id;

Sourced here.

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.