在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