doulai8405 2019-08-15 03:21
浏览 101

自定义分类图像 - 输出到单个产品页面

I'm trying to display images attached to custom taxonomies on the individual product pages. I've used the Custom Taxonomy plugin (https://wordpress.org/plugins/taxonomy-images/) and added the images using Advanced Custom Fields. I have 2 custom taxonomies called 'range_logo' and 'features', each with images assigned to them. I need to output the images to the single product page using the hook 'woocommerce_before_add_to_cart_form'.

As you can see below, I have tried outputting a line of text to test if the hook is working correctly, and it is, but the taxonomy image isn't shown when I add the code which is supplied with the Taxonomy Images plugin.

add_action( 
"woocommerce_before_add_to_cart_form","product_taxonomy_image");
function product_taxonomy_image () {
    echo 'Taxonomy image here';
print apply_filters( 'taxonomy-images-queried-term-image', '' );
}

Expected results are that the appropriate taxonomy image for the product would be displayed. Instead I got this error:

Notice: term_taxonomy_id is not a property of the current queried object. This usually happens when the taxonomy-images-queried-term-image-id filter is used in an unsupported template file. This filter has been designed to work in taxonomy archives which are traditionally served by one of the following template files: category.php, tag.php or taxonomy.php. Learn more about template hierarchy. in /var/sites/a/mysite.com/public_html/wpnew/wp-content/plugins/taxonomy-images/legacy/includes/public-filters.php on line 398

I'm using the Avada theme - maybe the layout of the theme's template files could be the reason for the error? I think I may need to use archive.php instead, but I don't have much experience with PHP.

I've tried a few possible solutions and have looked elsewhere for help, but I've hit a wall with getting this to work and I'm not sure what I should be trying next.

  • 写回答

1条回答 默认 最新

  • duanrebo3559 2019-08-15 04:15
    关注

    welcome to the community. First off, I would like to mention that you have written a bit confusing detail about the taxonomy images as you have mentioned using the plugin Taxonomy Images as well as Advanced custom fields

    If you are using the former plugin, you can do something as shown below:

    add_action("woocommerce_before_add_to_cart_form","product_taxonomy_image");
    function product_taxonomy_image () {
        print apply_filters( 'taxonomy-images-list-the-terms', '', array(
            'before'       => '<div class="my-custom-class-name">',
            'after'        => '</div>',
            'before_image' => '<span>',
            'after_image'  => '</span>',
            'image_size'   => 'detail',
            'taxonomy'     => 'product_cat',
        ) );
    }
    

    The above code will give you the images from the category associated with that product. If it is another custom taxonomy that you created, please change the value in taxonomy parameter.

    If you are using ACF you will have to get product terms first. Let me know if that's the case and I will share that code with you.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大