duan201444
2015-11-13 17:45
浏览 83
已采纳

运行循环后ACF未显示

In the below code, I call fields from Advanced Custom Fields plugin and only the first two show 'home_title' and 'home_content'. After these two I run two different loops to show the latest posts in a given category. After those loops run there are 4 more fields from ACF called. 'donate_title' , 'donate_content' , 'mission_title' , 'mission_content'. Which are not showing up (not pulling any content at all).

If I move these ACF before running the loops they all show up correctly. So I imagine there is a problem with these following the loops but cannot find the reason.

<div class="main-site">
<div class="home-title-1">
<?php the_field('home_title'); ?>
</div>
<div class="home-content-1">
<?php the_field('home_content'); ?>
</div>

<div class="home-boxes-cont">
<div class="box-left">
<?php
query_posts('cat=4&posts_per_page=1');
while (have_posts()) : the_post(); ?>
    <div class="bl-img">
    </div>
    <div class="bl-title">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
    <div class="bl-content">
    <?php the_excerpt(); ?>
    </div>
<?php endwhile; ?>
</div>

<div class="box-middle">
<?php
query_posts('cat=5&posts_per_page=1');
while (have_posts()) : the_post(); ?>
    <div class="bm-img">
    </div>
    <div class="bm-title">
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </div>
    <div class="bm-content">
    <?php the_excerpt(); ?>
    </div>
<?php endwhile; ?>
</div>

<div class="box-right">
    <div class="br-img">
    </div>
    <div class="br-title">
    <?php the_field('donate_title'); ?>
    </div>
    <div class="br-content">
    <?php the_field('donate_content'); ?>
    </div>
</div>
</div>

<div class="mission-title">
    <?php the_field('mission_title'); ?>
</div>
<div class="mission-content">
    <?php the_field("mission_content"); ?>
</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doucheyi1347 2015-11-13 21:31
    已采纳

    In order to get custom field data from the original post after altering the global post data with your query_posts() calls, you need to reset your post data with the wp_reset_query() function. Place this function after each loop -

    <?php while (have_posts()) : the_post(); ?>
    
    ...
    
    <?php endwhile; wp_reset_query(); ?>
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题