【问题标题】:get all variation images (additional variation images woocommerce plugin)获取所有变体图像(其他变体图像 woocommerce 插件)
【发布时间】:2018-07-05 08:27:50
【问题描述】:

我需要检索一个变体的所有图像,我可以使用 $variation = wc_get_product( $variation_id ); $updated_image_id = $variation->get_image_id('edit'); 获取第一张图像 但是如果我知道变体 id,如何获得所有额外的变体图像?

【问题讨论】:

  • 变体有几个图像?还是您想循环浏览变体?
  • 变体有几个图像。我想遍历单个变体的图像。
  • 尝试将$variation->get_image_id('edit')$variation->get_gallery_image_ids(); 切换应该会为您提供图库中的图像ID 数组。
  • 取决于您的 woocommerce 版本 - 如果是旧版本,请使用 $variation->get_gallery_attachment_ids();
  • 图库是空的,图像在变体中。它们存储在数据库表 wp_postmeta 中,键为“_wp_attached_file”,但具有自己的帖子 ID,我无法使用变体 ID 加入。

标签: php wordpress plugins woocommerce


【解决方案1】:

你可以试试这个——虽然没有测试。

$args = array(
    'post_parent' => $variation_id,
    'post_type' => 'attachment',
    'post_mime_type' => 'image' 
);
$images = get_children( $args );

if ( empty($images) ) {
    // no attachments here
} else {
    foreach ( $images as $attachment_id => $attachment ) {
        //Do whatever here
        echo wp_get_attachment_image( $attachment_id, 'full' );
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2022-10-19
    • 2022-06-13
    • 1970-01-01
    相关资源
    最近更新 更多