我要显示五个帖子(或更多),但是我想在两列中显示两个帖子,然后在一个列中显示下一个帖子。我正在尝试使用下面的代码(wp),但这无法正常工作。
我的HTML:
<div class="inner col-xs-12 col-sm-12 col-md-6 col-lg-6 pl-5 pr-5"></div>
<div class="inner col-xs-12 col-sm-12 col-md-6 col-lg-6 pl-5 pr-5"></div>
<div class="inner w-full col-xs-12 col-sm-12 col-md-12 col-lg-12 pl-5 pr-5"></div>
<div class="inner col-xs-12 col-sm-12 col-md-6 col-lg-6 pl-5 pr-5"></div>
<div class="inner col-xs-12 col-sm-12 col-md-6 col-lg-6 pl-5 pr-5"></div>
<div class="inner w-full col-xs-12 col-sm-12 col-md-12 col-lg-12 pl-5 pr-5"></div>
我的WordPress代码:
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
$query = new WP_Query(array('post_type'=>'history', 'posts_per_page' => -1));
$post_count = 0;
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post();
if($post_count <= 2) {
?>
<div class="inner col-xs-12 col-sm-12 col-md-6 col-lg-6 pl-5 pr-5">
</div>
<?php } else ?>
<div class="inner w-full col-xs-12 col-sm-12 col-md-12 col-lg-12 pl-5 pr-5">
</div>
<?php $post_count++; endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php endif; ?>
答案 0 :(得分:0)
您正在检查帖子数是否小于或等于2,因此它将截断为3,您可以使用以下3的倍数
您可以尝试
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
$query = new WP_Query(array('post_type'=>'history', 'posts_per_page' => -1));
$post_count = 0;
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post();
if($post_count % 3 != 0) {
?>
<div class="inner col-xs-12 col-sm-12 col-md-6 col-lg-6 pl-5 pr-5">
</div>
<?php } else ?>
<div class="inner w-full col-xs-12 col-sm-12 col-md-12 col-lg-12 pl-5 pr-5">
</div>
<?php $post_count++; endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php endif; ?>
答案 1 :(得分:0)
尝试下面的代码
if ( get_query_var('paged') ) $paged = get_query_var('paged');
$query = new WP_Query(array('post_type'=>'testpost', 'posts_per_page' => -1));
$post_count = 1;
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post();
if($post_count % 3 != 0) {
?>
<div class="inner col-xs-12 col-sm-12 col-md-6 col-lg-6 pl-5 pr-5">
</div>
<?php } else{ ?>
<div class="inner w-full col-xs-12 col-sm-12 col-md-12 col-lg-12 pl-5 pr-5">
</div>
<?php } $post_count++; endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<?php endif; ?>