在WooCommerce挂钩中显示高级自定义字段(ACF)值 [英] Displaying Advanced Custom Field (ACF) value in a WooCommerce hook

查看:93
本文介绍了在WooCommerce挂钩中显示高级自定义字段(ACF)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Woocommerce中显示高级自定义字段(ACF)中的值,并且在此函数中使用了以下代码:

I want to display the value from an Advanced Custom Field (ACF) in Woocommerce and I am using this code in this function hooked:

add_action( 'woocommerce_single_product_summary', 'charting', 20 );

function charting() {
    if( get_field('size_chart') ) { 
       echo '<p><a href="'.the_field('size_chart').'" data-rel="prettyPhoto">size guide</a></p>';
    }
    return;
}

但是它不起作用,它在href上方显示自定义字段值(大小指南),并且href像这样是空的:

But it is not working, it is displaying the custom field value above the href (size guide) and the href is empty like this:

<a href="" data-rel="prettyPhoto">size guide</a>


推荐答案

您的问题是您不能使用 echo 与ACF the_field('my_field ') ,因为使用 the_field ('my_field') 就像使用 echo get_field('my_field') ,因此您正在尝试 echo echo 。而是使用 get_field('my_field') 在您的代码中以这种方式:

Your problem is that you can't use echo with ACF the_field('my_field') , because when using the_field('my_field') is just like using echo get_field('my_field'), so you are trying to echo an echo. Instead use get_field('my_field') this way in your code:

add_action( 'woocommerce_single_product_summary', 'charting', 20 );

function charting() {
    if( !empty( get_field('size_chart') ) ) { // if your custom field is not empty…
        echo '<p><a href="' . get_field('size_chart') . '" data-rel="prettyPhoto">size guide</a></p>';
    }
    return;
}

之后,我添加 empty()函数在您的情况下...

After, I have add empty() function in your condition…

您也可以尝试返回而不是 echo

    return '<p><a href="' . get_field('size_chart') . '" data-rel="prettyPhoto">size guide</a></p>';

参考:

  • ACF the_field
  • ACF get_field

这篇关于在WooCommerce挂钩中显示高级自定义字段(ACF)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆