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 } ?>
问题截图

已尝试的解决方法
- 将
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
相关产品推荐
相关产品推荐

