opportunityr
opportunityr

Reputation: 183

Retrieving product gallery image thumbnails in Woocommerce

I'm looking for a function that gets me the thumbnails of gallery images, not the full image. I currently retrieve them using $product->get_gallery_attachment_ids();.

Code in question:

$attachment_ids = $product->get_gallery_attachment_ids();

$image_link = wp_get_attachment_url( $attachment_ids[0] );

echo "<img class='back-thumb' src='" . $image_link . "'></img>";

Upvotes: 2

Views: 3085

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253804

You can use dedicated Woocommerce wc_get_gallery_image_html() function with the WC_Product method get_gallery_image_ids() like:

if ( $attachment_ids = $product->get_gallery_image_ids() ) {
    foreach ( $attachment_ids as $attachment_id ) {
        echo wc_get_gallery_image_html( $attachment_id );
    }
}

or like in your code:

if ( $attachment_ids = $product->get_gallery_image_ids() )
    echo wc_get_gallery_image_html( $attachment_ids[0] );

Tested and works.


Changing thumbnail dimensions and cropping: See this documentation section

Upvotes: 5

Related Questions