我花了一段时间才意识到这一点,但最终,我成功了。如果其他人希望实现类似的布局,我将在此处分享我的解决方案:
<ul class="post preview grid grid--blog">
<?php $do_not_duplicate = 0 ?>
<?php if(!is_paged()): ?>
<?php $args = array(
\'category_name\' => \'featured\',
\'posts_per_page\' => 1
);
$featured = new WP_Query( $args );
while ( $featured->have_posts() ) : $featured->the_post();
$do_not_duplicate = $post->ID; ?>
<li class="grid__item grid__item-featured"><?php get_template_part( \'entry\' ); ?></li>
<?php endwhile;
endif;
wp_reset_query(); ?>
<?php
$featuredid = get_cat_ID( \'Featured\' );
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
if(!is_paged()) {
$postsargs = array(
\'posts_per_page\' => 4,
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => 1,
\'cat\' => - $featuredid,
\'paged\' => $paged
);
$allposts = new WP_Query( $postsargs );
} else {
$secondPageArgs = array(
\'posts_per_page\' => 6,
\'post_status\' => \'publish\',
\'ignore_sticky_posts\' => 1,
\'cat\' => - $featuredid,
\'paged\' => $paged
);
$allposts = new WP_Query( $secondPageArgs );
}
if ( $allposts->have_posts() ) :
while ( $allposts->have_posts() ) : $allposts->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<li class="grid__item"><?php get_template_part( \'entry\' ); ?></li>
<?php endwhile;
endif;
wp_reset_query(); ?>
</ul>
所以
$do_not_duplicate
确保在列出所有其他帖子时,不要重复第二个查询中第一个查询中的特色帖子。
然后包装我们的第二个查询$allposts
IF语句中的参数,用于检查这些帖子将显示在哪个页面上。如果我们在第一个博客页面上,\'posts_per_page\' => 4
将被调用,否则\'posts_per_page\' => 6
将在所有其他页面上调用。
希望有帮助!