2015-11-13 51 views
0

在下面的代码中,我调用了高级自定义字段插件的字段,只有前两个显示'home_title'和'home_content'。在这两个之后,我运行了两个不同的循环来显示给定类别中的最新帖子。在这些循环运行之后,还有4个ACF调用的字段。 'donate_title','donate_content','mission_title','mission_content'。没有显示出来(根本不拉取任何内容)。运行循环后没有显示ACF

如果我在运行这些循环之前移动这些ACF,它们都会正确显示。所以我想象下面的循环有一个问题,但找不到原因。

<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> 

+0

是否在选项页上设置了最后四个字段中的任何一个? – danjah

+0

他们都有相同的设置,前两个 – Ryeboflaven

+0

好的。尝试将它们全部存储在代码顶部的变量中,然后在需要它们的地方回显出来。 – danjah

回答

0

为了使用query_posts()调用更改全局发布数据后从原始帖子中获取自定义字段数据,您需要使用wp_reset_query()函数重置发布数据。在每个循环之后放置此功能 -

<?php while (have_posts()) : the_post(); ?> 

... 

<?php endwhile; wp_reset_query(); ?> 
+0

谢谢约翰尼,我正在尝试一些其他的选择,但这是最简单的,并得到我所需要的。 – Ryeboflaven

0

您正在改变全球wp_query变量。当你这样做,结果不是is_single(),那么你不能再拉任何ACF设置。

无论是wp_query恢复到初始设置为页面或VARS存储在数组中所做的任何wp_query变化中,如在后面的代码需要检索它们。

0

虽然我同意Scriptonomy,但您应该只使用get_posts()。这正是这个功能设计的目的......在主循环之外的自定义循环。您几乎不需要修改全局wp_query变量。

如果你仍然想使用the_permalink()the_title()没有通过文章ID,然后页面向下滚动到标有节“访问所有数据后”,你会看到如何使用setup_postdata(),使其更容易。