0

我有一个静态页面,其中有一个高级自定义字段 (ACF) 的“中继器”区域和一些用于拉入一些自定义帖子的代码。

我在页面顶部有这段代码。

<?php get_header(); the_post(); query_posts('post_type=team_profiles') ?>

如果这样离开,我会看到我的帖子,但看不到来自 ACF 的数据。如果我取出以下部分:

query_posts('post_type=team_profiles')

我看到了 ACF 字段,但没有看到发布数据。

我的问题首先是为什么会发生这种情况,其次是如何将它们放在同一页面上?

我的静态页面中 ACF 和 POSTS 的代码如下:

ACF 中继器字段

    <?php if( get_field('about_sections') ): ?>

    <?php while( has_sub_field('about_sections') ): ?>

    <div class="asset image">

    <?php the_sub_field('about_section'); ?>

    </div> <!-- asset image -->

    <?php endwhile; ?>

    <?php endif; ?>

帖子

    <?php if ( have_posts() ) : ?>

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

    <?php get_template_part( 'content-single-team-profile', get_post_format() ); ?>

    <?php endwhile; ?>

    <?php endif; ?>
4

1 回答 1

0

发生这种情况是因为调用 get_posts() 时主查询被覆盖。您可以:

  • 在从静态页面(转发器字段值等)获得所需的一切之后调用 get_posts( )。

或者

  • 在尝试从静态页面获取值之前调用 wp_reset_query() 。这将为该页面的主查询设置原始参数。

看看这个:http ://codex.wordpress.org/Function_Reference/wp_reset_postdata

这些都是简单的解决方案。更复杂和更好的将涉及创建一个 WP_Query 对象并迭代它的帖子,而不是依赖于全局变量。

于 2013-10-02T14:14:29.567 回答