WordPress-在single.php上按产品ID获取数组中的产品库链接

时间:2019-01-04 06:02:36

标签: php wordpress woocommerce

我正在用WordPress从头开始设计模板,我在按产品ID在single.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));

显示为空数组,但我需要图像路径。

1 个答案:

答案 0 :(得分:0)

我认为这段代码可以解决问题。

<?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');

        }
?>