在 Woocommerce 中检索产品图库图像缩略图

Retrieving product gallery image thumbnails in Woocommerce

我正在寻找一种功能,它可以为我提供图库图像的 缩略图 ,而不是完整图像。我目前使用 $product->get_gallery_attachment_ids();.

检索它们

有问题的代码:

$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>";

您可以使用专用的 Woocommerce wc_get_gallery_image_html() function with the WC_Product method get_gallery_image_ids(),例如:

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

或者像你的代码一样:

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

已测试并有效。


更改缩略图尺寸和裁剪: See this documentation section