you can use shomething like this:
<?php
$gallery = $product->get_gallery_image_ids();
?>
<div>
<?php
foreach ( $gallery as $imgSource ) {
echo wp_get_attachment_image( $imgSource );
}
?>
</div>
for more info and see accepted parameters:
wp_get_attachment_image
get_post_gallery_images() is specifically for getting galleries which are defined with the [gallery] shortcode (old style gallery in “classic” editor). The block editor galleries do not use this shortcode, so the function is ineffective for new style galleries.
I’m not sure if there is an equivalent function for new style galleries. You can get new style gallery attachment IDs by using preg_match() on post content. Use a regexp that will capture the string of IDs in the block’s comment header. Explode the string into an array, then foreach the array and use wp_get_attachment_image() like Nik illustrated. The $product->get_gallery_image_ids() part is meant for product galleries, which it’s not clear if you are using or not. What I’ve described is for block editor galleries.
I knew what bcworkz said about it not working with the block editor galleries (I’ve had problems with this function before), so now Im just confused as to why it worked when I used it in v 5.1.6.
Anyway, with a combination of the two answers I threw together this which seems to work fine. The theme I’m developing is for myself, so I know I can always guarantee that the first gallery will the be one I need (and I know it will always be there), which is why I was fine hardcoding it.
$postBlocks = parse_blocks($post->post_content);
$firstGalleryIds = $postBlocks[0]["attrs"]["ids"];
$previewImages = [];
foreach ( $firstGalleryIds as $imgSource ) {
$previewImages[] = wp_get_attachment_image_src( $imgSource, $size = 'large' )[0];
}