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;
}
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>
答案 0 :(得分:3)
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;
}
After, I have add empty()
function in your condition…
You can also try to return
it instead of echo
:
return '<p><a href="' . get_field('size_chart') . '" data-rel="prettyPhoto">size guide</a></p>';
Reference:
答案 1 :(得分:1)
我使用了这段代码并且它在本地工作得很好但是当我在服务器上传功能文件时它没有工作并且它给服务器错误500,所以我不得不再次删除此代码