我正试图使用一个自定义查询创建一个WP循环,以显示一个自定义的post类型—使用Bootstrap 4标记。让我感到复杂的是,我希望每行的帖子数量按以下方式交替排列:
第1行-一栏-全屏宽度第2行-四栏-col-md-6第3行-六栏-col-md-4目前我正在运行三个单独的循环,这会弄乱分页。i、 e.单击分页时,页面内容保持不变,但url指示用户位于第二页。感谢您的帮助。
<!-- Loop 1 -->
<?php
$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;
$args=array(
\'post_type\' => \'workshop\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 1,
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
// modified to work with 3 columns
// output an open <div>
if($i % 1 == 0) { ?>
<div class="row">
<?php
}
?>
<!-- ROW 1 - FULL SCREEN WIDTH MARKUP -->
</div><!--/.row-->
<?php $i++;
if($i != 0 && $i % 1 == 0) { ?>
</div> <!-- End Container -->
<div class="clearfix"></div>
<?php
} ?>
<?php
endwhile;
}
wp_reset_postdata();
?>
<!-- Loop Two -->
<?php
$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;
$args=array(
\'post_type\' => \'workshop\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 4,
\'offset\' => 1,
\'paged\' => $paged
);
$my_query = null;
$do_not_duplicate[] = $post->ID;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
// modified to work with 3 columns
// output an open <div>
if($i % 2 == 0) { ?>
<div class="row no-gutters">
<?php
}
?>
<!-- ROW 2 - [FOUR POSTS] MARKUP HERE -->
<?php $i++;
if($i != 0 && $i % 2 == 0) { ?>
</div><!--/.row-->
<?php
} ?>
<?php
endwhile;
}
wp_reset_query();
?>
$args=array(
\'post_type\' => \'workshop\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 6,
\'offset\' => 5,
\'paged\' => $paged
);
$my_query = null;
$do_not_duplicate[] = $post->ID;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$i = 0;
while ($my_query->have_posts()) : $my_query->the_post();
// modified to work with 3 columns
// output an open <div>
if($i % 3 == 0) { ?>
<div class="row no-gutters">
<?php
}
?>
<!-- ROW 3 - [Six Posts] Markup -->
<?php $i++;
if($i != 0 && $i % 3 == 0) { ?>
</div><!--/.row-->
<?php
} ?>
<?php
endwhile;
}
wp_reset_query();
?>
最合适的回答,由SO网友:mrben522 整理而成
要做到这一点,最简单但有点麻烦的方法是在循环之前初始化一个计数器变量,然后用它来判断你在写什么帖子。大概是这样的:
$paged = (get_query_var(\'paged\')) ? absint(get_query_var(\'paged\')) : 1;
$args = array(
\'post_type\' => \'workshop\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 11,
\'paged\' => $paged,
);
$customQuery = new WP_Query($args);
$counter = 1;
if ($customQuery->have_posts()) :
while ($customQuery->have_posts()) : $customQuery->the_post();
if ($counter === 1) { ?>
<div class="row">
<!-- ROW 1 - FULL SCREEN WIDTH MARKUP -->
</div><!--/.row-->
<?php
} elseif ($counter <= 5) {
if ($counter === 2) { ?>
<div class="row no-gutters">
<?php }
?>
<!-- ROW 2 - [FOUR POSTS] MARKUP HERE -->
<?php if ($counter === 5) { ?>
</div><!--/.row-->
<?php }
} else {
if ($counter === 6) { ?>
<div class="row no-gutters">
<?php } ?>
<!-- ROW 3 - [Six Posts] Markup -->
<?php if ($counter === 11) { ?>
</div><!--/.row-->
<?php }
};
$counter++;
endwhile;
endif;