php 根据id获取src,Woocommerce通过产品ID获取图片库

如何以编程方式显示产品库中的图像?我看到了显示特色图片的选项;但不显示产品ID的所有图像。

是否有可能做到这一点?

我尝试这样做, 但是在Woocommerce中无法通过ID从产品库中获取图像。

$gallery = get_post_gallery_images(724);

$image_list = '

  • ';

foreach( $gallery as $image ) {// Loop through each image in each gallery

$image_list .= '

'%20.%20str_replace('-150x150',%20'',%20%24image)%20.%20'%20';

}

$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 ''%20.%20%24image_link%20.%20'';

}

?>

//Get all image by tag

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值