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

ACF的the_field函数在WordPress while循环中无法生效

WordPress中WP_Query查询自定义文章类型时ACF字段无法输出

在使用WP_Query查询post_type为tariffs的自定义文章类型时,循环内的ACF函数the_field('tariff_price_kn')无法输出内容,但the_title()可以正常显示,将该函数替换为普通echo语句也能正常运行。

无效核心代码

<span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>

完整查询及循环代码

<?php 

$args = array(
  'post_type' => 'tariffs',
  'posts_per_page' => 3,
  'type_of_site' => 'landing_page_type',
);

$webTariffs = new WP_Query($args);

while ($webTariffs->have_posts()) {
  $webTariffs->the_post();?>

  <div class="pricing-item">
    <div class="pricing-item-header">
      <h3><?php the_title() ?></h3>
      <span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>
    </div>
  </div>
<?php } ?>

问题截图

ACF字段无法输出截图

已尝试的解决方法

  • 将the_field('tariff_price_kn')替换为echo('hi'),代码可正常运行
  • 尝试传入$post_id调用:the_field('tariff_price_kn', $post_id)
  • 尝试使用get_field('tariff_price_kn')赋值后echo,循环前后均无效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 21:25:25