You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中WooCommerce短代码调用ACF产品ID变量失效求助

问题解决:ACF关联产品ID无法正确传入WooCommerce短代码

问题出在ACF函数的使用上:the_field()是直接输出字段值,不会返回值,所以你执行$value = the_field('powiazane_produkty');时,$value实际上是空的。你看到的“单独输出变量显示正常”其实是the_field()直接打印的结果,并非$value本身有值,这就导致短代码的ids参数没有正确传入产品ID,WooCommerce只能显示随机产品。

修正代码

将the_field()替换为ACF的get_field()函数,它会返回字段的原始值,这样就能正确获取产品ID并传入短代码:

<?php 
$value = get_field('powiazane_produkty');
echo do_shortcode('[products limit="1" per_page="1" ids="' . $value . '"]'); 
?>

内容的提问来源于stack exchange,提问作者Krzysztof Szatecki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 14:37:03