在WordPress循环中获得每秒钟的帖子


Get every second post in WordPress loop

我需要在两列中分开循环,并希望跳过每个循环中的每一秒循环。最后,我需要一个这样的视图:

循环1

    1
  • 文章
  • 文章 3
  • 5
  • 文章

循环2

    2
  • 文章
  • 4
  • 文章
  • 文章 6

这可能吗?

我的当前循环:

<?php $args = array (
                'nopaging'               => true,
                'posts_per_page'         => '999',
                'ignore_sticky_posts'    => false,
            );
            $query_row_1 = new WP_Query( $args ); if ( $query_row_1->have_posts() ) : ?>
                <?php while ( $query_row_1->have_posts() ) : $query_row_1->the_post(); ?>
                    <?php the_title(); ?>
                <?php endwhile; ?>
            <?php else : endif; wp_reset_postdata(); ?>

正如我已经评论过的:

你可以这样声明一个计数器变量:

$counter = 0;

在while循环的开头插入以下四行:

$counter++;
if($counter % 2)
{
    continue;
}

将计数器加1,并检查计数器是否为偶数。由于结果只有0或1,因此可以直接在if配置中使用它,因为这表示真或假。如果是奇数,while循环跳转到下一次运行,如果是偶数,循环的其余部分继续。如果您想切换偶数和关闭,请输入If语句,如if(!($counter % 2))