dongqing7789 2018-12-14 15:14
浏览 105
已采纳

按属性ID获取WooCommerce属性缩略图URL(WooCommerce Product Search插件)

I've already spent a couple of hours googling and already tried many things. The closer I could get to my solution was using this code:

// With this I successfully get an Array with attribute ID
$carimg_attr = wc_get_product_terms( get_the_id(), 'pa_carimg', 'thumbnail' );

// This echo actually returns the correct ID
echo $carimg_attr[0]->term_id;

// In this echo, I get it empty but this was supposed to show me metas from the attribute (at least as far as other online answers was saying)
echo get_woocommerce_term_meta( $carimg_attr[0]->term_id, 'pa_carimg', true );

// Printing this one, I get actually all fields EXCEPT the one I need: thumbnail
print_r(wc_get_product_terms( get_the_id(), 'pa_carimg', array( 'fields' => 'all' ) ));

// This was just a last desperate try and doesn't work either
$carimg_Img = wp_get_attachment_image_src( $carimg_attr[0]->term_id ); ?>
<img src="<?php echo $carimg_Img[0]; ?>">

I'm just out of ideas! :(

EDIT:

I was just being dumb here! Thanks to @LoicTheAztec comment I just noticed this is something from official WooCommerce Product Search plugin.

  • 写回答

1条回答 默认 最新

  • duancutan4770 2018-12-14 16:01
    关注

    Solution for this problem:

    <?php
    $carimg_attr = wc_get_product_terms( get_the_id(), 'pa_carimg', 'thumbnail' );
    $imgAtt_id = get_woocommerce_term_meta( $carimg_attr[0]->term_id, 'product_search_image_id', true );
    $imgAtt_url = wp_get_attachment_image_src( $imgAtt_id, 'medium', false );
    ?>
    <img src="<?php echo $imgAtt_url[0]; ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题