首页 > 解决方案 > ACF 链接到 woocommerce 单页中的按钮

问题描述

我正在努力将 acf URL 字段添加到 woo 单页中的按钮。有什么帮助吗?

这就是我到目前为止所拥有的。

add_action( 'woocommerce_after_add_to_cart_button', 'display_acf_field_under_cart' );
/*
 * Content below "Add to cart" Button.
 */
function display_acf_field_under_cart() {

  echo '<div>

  <a href=" the_field("button_url"); "><button class="gc-outline">Tested button</button></a>

  </div>';
}

然而它不工作....

谢谢

标签: wordpressfunctionwoocommercehook-woocommerce

解决方案


@LoicTheAztec 评论可以帮助您实现目标,因为它纠正了php代码中的错误。

但为了更进一步,我添加了一个条件,如ACF 文档button_url中所示,这样如果产品的字段为空,您就不会显示空按钮。

add_action( 'woocommerce_after_add_to_cart_button', 'display_acf_field_under_cart' );
/*
 * Content below "Add to cart" Button.
 */
function display_acf_field_under_cart() {
$button_url = get_field('button_url');
if ($button_url) {
  echo '<div>
  <a href="'.$button_url.'"><button class="gc-outline">Tested button</button></a>
  </div>';
  }else {
    echo '';
  }
}

推荐阅读