请尝试在第二个查询之前添加以下内容:
wp_reset_postdata();
它将恢复您的
$post global,这将有助于您第二次出现
have_posts
和
the_post
正常工作。
这就是我通常在一个页面上进行多个查询的方式。我正在手动保存和恢复$post 全球,但任何一种方式都应该很好。
<?php if(have_posts()) while(have_posts()): the_post(); ?>
<article class="single-article" id="article-<?= $post->ID ?>">
<!-- output the page content here -->
</article>
<? endif ?>
<?php endwhile; ?>
<!-- start first custom query output -->
<?php
global $post;
$tmp_post = $post;
$args = array(
\'meta_key\' => \'featured\',
\'meta_value\' => \'1\',
\'post_status\' => \'publish\',
\'post_type\' => \'post\' );
$featured_posts = get_posts( $args );
if( !empty($featured_posts) ): ?>
<section id="featured-posts">
<? foreach( $featured_posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<!-- and so on -->
<?php endforeach; ?>
</section>
<?php endif; $post = $tmp_post; /* restore global post variable */ ?>
<!-- start second custom query output -->
<?php
$tmp_post = $post;
$story_args = array(
\'meta_key\' => \'featured\',
\'meta_value\' => \'1\',
\'post_status\' => \'publish\',
\'post_type\' => \'story\' );
$story_posts = get_posts( $story_args );
if( !empty($story_posts) ): ?>
<section id="stories">
<? foreach( $story_posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<!-- and so on -->
<?php endforeach; ?>
</section>
<?php endif; $post = $tmp_post; /* restore global post variable */ ?>