Show a WooCommerce product gallery in any template
On the single product page WooCommerce shows the main image and the gallery with its own zoom and slider scripts. On a landing page, in a quick-view popup or in a custom product block, you often need the same images in your own markup. The product object gives you the IDs, and WordPress’ image functions do the rest.
Problem: A landing page should show the main image and the gallery thumbnails of a product, with the right sizes and alt texts, outside the single product template.
Solution: Get the IDs with $product->get_image_id() and $product->get_gallery_image_ids(), print them with wp_get_attachment_image() in WooCommerce’s image sizes, and fall back to wc_placeholder_img() when the product has no images.
The function
<?php
function ha_product_gallery( $product_id ) {
$product = wc_get_product( $product_id );
if ( ! $product ) {
return '';
}
$ids = array_filter( array_merge( array( $product->get_image_id() ), $product->get_gallery_image_ids() ) );
if ( ! $ids ) {
return '<div class="ha-gallery">' . wc_placeholder_img( 'woocommerce_single' ) . '</div>';
}
$main = array_shift( $ids );
$html = '<div class="ha-gallery">';
$html .= '<div class="ha-gallery__main">' . wp_get_attachment_image( $main, 'woocommerce_single', false, array( 'loading' => 'eager' ) ) . '</div>';
if ( $ids ) {
$html .= '<ul class="ha-gallery__thumbs">';
foreach ( $ids as $id ) {
$html .= sprintf(
'<li><a href="%s">%s</a></li>',
esc_url( wp_get_attachment_image_url( $id, 'woocommerce_single' ) ),
wp_get_attachment_image( $id, 'woocommerce_gallery_thumbnail' )
);
}
$html .= '</ul>';
}
return $html . '</div>';
}
add_shortcode( 'ha_product_gallery', function ( $atts ) {
$atts = shortcode_atts( array( 'id' => 0 ), $atts );
return ha_product_gallery( absint( $atts['id'] ) );
} );
[ha_product_gallery id="42"] now shows the gallery anywhere. The alt texts come from the media library; if they are empty, set them there instead of inserting the product name in code.
The image sizes
WooCommerce registers its own sizes and lets the shop owner change them in Appearance → Customize → WooCommerce → Product Images:
woocommerce_single: the main image on the product pagewoocommerce_thumbnail: images in the shop gridwoocommerce_gallery_thumbnail: the small gallery thumbnails
Using these sizes keeps your gallery consistent with the rest of the shop and reuses files that already exist.
Switching images
A few lines of JavaScript swap the main image when a thumbnail is clicked; the link already points to the large version:
document.addEventListener( 'click', function ( event ) {
var link = event.target.closest( '.ha-gallery__thumbs a' );
if ( ! link ) {
return;
}
event.preventDefault();
var main = link.closest( '.ha-gallery' ).querySelector( '.ha-gallery__main img' );
main.removeAttribute( 'srcset' );
main.src = link.href;
} );
Removing srcset makes the browser use the new src; otherwise it would keep showing the old image from its own list.
The original gallery
If you want WooCommerce’s gallery with zoom and lightbox on a custom page, the product page features have to be enabled by the theme (add_theme_support( 'wc-product-gallery-zoom' ), wc-product-gallery-lightbox, wc-product-gallery-slider) and the scripts loaded, which WooCommerce does only on product pages. For a landing page, the simple gallery above is usually the better choice.
NOTE: The first image is the most important one for page speed. Load it eagerly (as above) and leave the thumbnails to native lazy loading, especially when the gallery is at the top of the landing page.