soft dev
soft dev

Reputation: 41

How to foreach all Woocommerce product images

I wanna create my custom product gallery, and I need to loop through all images featured image and attachment images. I need to create array contains featured image and attachment images. I need the code to be like that.

foreach( $images as $image_src_url ) 
{
  echo '<img src="'.$image_src_url.'">';
}

Upvotes: 1

Views: 4317

Answers (1)

mujuonly
mujuonly

Reputation: 11861

<?php
global $product;

$attachment_ids  = $product->get_gallery_image_ids();
$image_urls      = array();
$image_id        = $product->get_image_id();
if ( $image_id ) {
    $image_url = wp_get_attachment_image_url( $image_id, 'full' );

    $image_urls[ 0 ] = $image_url;
}

foreach ( $attachment_ids as $attachment_id ) {
    $image_urls[] = wp_get_attachment_url( $attachment_id );
}

foreach ( $image_urls as $image_src_url ) {
    echo '<img src="' . $image_src_url . '">';
}
?>

Try this code snippet.

Upvotes: 2

Related Questions