我正在WordPress中从头开始设计模板,我在按产品ID获取产品图像/图库时遇到问题.php这是我的代码:
function GetImageUrlsByProductId( $productId){
$product = new WC_product($productId);
$attachmentIds = $product->get_gallery_image_ids();
$imgUrls = array();
foreach( $attachmentIds as $attachmentId )
{
$imgUrls[] = wp_get_attachment_url( $attachmentId );
}
return $imgUrls;
}
$id = the_ID();
echo $id."<br/>";
$title = get_the_title();
echo $title."<br/>";
print_r(GetImageUrlsByProductId($id));
它显示了空数组,但我想要图像路径。
我认为这段代码可以解决问题。
<?php
$product_id = get_the_ID();
$product = new WC_product($product_id);
$attachment_ids = $product->get_gallery_attachment_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');
}
?>