wordpress - 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>';
}
然而它不工作....
谢谢
解决方案
@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 '';
}
}
推荐阅读
- python - 使用 Geopandas/GeoPlot 生成 GeoTiff
- java - 如何通过ID打开特定活动
- azure - 保护 Azure API 管理上的端点,以便只有应用程序(无需登录)可以调用它
- sql - 使用SQL查找多列行中的最小值
- c# - Oledb OledbDataAdapter 和 CommandBuilder 错误 Syntaxfehler in der INSERT INTO-Anweisung
- spring-boot - spring-kafka 中的消费者错误处理中的重试尝试计数不起作用
- python - 使用 pyomo 模型优化功能
- python - 为什么我的 python 代码不输出浮点数?
- kubernetes - Jenkins-X 3 operator install 无法下载“jx3/jx-git-operator”
- vb.net - 转义表示 sql 查询的字符串中的双引号字符