【问题标题】:How to foreach all Woocommerce product images如何获取所有 Woocommerce 产品图片
【发布时间】:2021-01-22 19:13:37
【问题描述】:

我想创建我的自定义产品库,并且我需要遍历所有带有图像和附件图像的图像。 我需要创建包含特色图像和附件图像的数组。 我需要这样的代码。

foreach( $images as $image_src_url ) 
{
  echo '<img src="'.$image_src_url.'">';
}

【问题讨论】:

    标签: woocommerce


    【解决方案1】:
    <?php
    global $product;
    
    $attachment_ids  = $product->get_gallery_image_ids();
    $image_urls      = array();
    $image_id        = $product->get_image_id();
    if ( $image_id ) {
        $image_url = wp_get_attachment_image_url( $image_id, 'full' );
    
        $image_urls[ 0 ] = $image_url;
    }
    
    foreach ( $attachment_ids as $attachment_id ) {
        $image_urls[] = wp_get_attachment_url( $attachment_id );
    }
    
    foreach ( $image_urls as $image_src_url ) {
        echo '<img src="' . $image_src_url . '">';
    }
    ?>
    

    试试这个代码 sn-p。

    【讨论】:

    • 此 sn-p 仅获取产品附件“产品库”。我还需要包含特色图片“产品图片”
    • $image = wp_get_attachment_image_src(get_post_thumbnail_id($product->ID), 'single-post-thumbnail' ); // 特色图片
    • 如何将特色图片和附件图片合并到一个数组中,并foreach这个数组得到所有产品图片(特色+附件)?
    • @mujuonly,我添加了你的答案,我正在获取产品图库图片但没有获取特色图片,我需要两者
    • $attachment_ids = $product->get_gallery_image_ids(); $image_urls = 数组(); if ( $product->get_image_id() ) { $image_urls[ 0 ] = wp_get_attachment_url( $product->get_image_id() ); } foreach ( $attachment_ids 作为 $attachment_id ) { $image_urls[] = wp_get_attachment_url( $attachment_id ); }
    猜你喜欢
    • 1970-01-01
    • 2015-02-26
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-12-26
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多