首页 文章

在WooCommerce挂钩中显示高级自定义字段(ACF)值

提问于
浏览
2

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

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为空,如下所示:

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

2 回答

  • 3

    你的问题是你不能使用 echo 与ACF the_field('my_field'),因为当使用the_field('my_field')就像使用echo get_field('my_field'),所以你试图 echo 一个 echo . 而是在代码中以这种方式使用get_field('my_field')

    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() 功能......

    您也可以尝试 return 而不是 echo

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

    参考:

  • 1

    我使用这个代码,它在本地运行良好,但当我在服务器上传功能文件时,它没有工作,它给服务器错误500,所以我不得不再次删除此代码

相关问题