如何以编程方式显示产品库中的图像?我看到了显示特色图片的选项;但不显示产品ID的所有图像。
是否有可能做到这一点?
我尝试这样做, 但是在Woocommerce中无法通过ID从产品库中获取图像。
$gallery = get_post_gallery_images(724);
$image_list = '
- ';
foreach( $gallery as $image ) {// Loop through each image in each gallery
$image_list .= '
';}
$image_list .= '
';echo $image_list;
?>
#1
我已经测试过了
$product_id = '14';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();
foreach( $attachment_ids as $attachment_id )
{
// Display the image URL
echo $Original_image_url = wp_get_attachment_url( $attachment_id );
// Display Image instead of URL
echo wp_get_attachment_image($attachment_id, 'full');
}
?>
#2
使用get_gallery_image_ids()获取自woocommerce 3.0.0起已弃用的Gallary Attechment ID bcoz get_gallery_attachment_ids()
#3
只是为了澄清, 以上答案将获得除缩略图(主)图像以外的所有图像。
要添加缩略图, 请使用以下命令:
$product_id = '652';
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_image_ids();
//这会将缩略图ID添加为数组的第一个元素
array_unshift($attachment_ids, get_post_thumbnail_id($product_id));
print_r($attachment_ids);
#4
$attachment_ids = $product->get_gallery_attachment_ids();
foreach( $attachment_ids as $attachment_id ) {
$image_link =wp_get_attachment_url( $attachment_id );
//Get image show by tag
echo '';
}
?>
//Get all image by tag