在 WordPress 循环中获取每秒 post
Get every second post in WordPress loop
我需要将循环分成两列,并希望跳过每个循环中的每个第二个循环。最后我需要这样的视图:
循环 1
- Post 1
- Post 3
- Post 5
循环 2
- Post 2
- Post 4
- Post 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;
}
这会将计数器加一并检查计数器是否为偶数。由于结果只有 0 或 1,因此可以直接在 if 条件中使用,因为这代表真或假。
如果它是奇数,则 while 循环跳转到下一个 运行,如果它是偶数,则循环的其余部分继续。如果你想切换甚至关闭,请像这样制作 if 语句 if(!($counter % 2))
我需要将循环分成两列,并希望跳过每个循环中的每个第二个循环。最后我需要这样的视图:
循环 1
- Post 1
- Post 3
- Post 5
循环 2
- Post 2
- Post 4
- Post 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;
}
这会将计数器加一并检查计数器是否为偶数。由于结果只有 0 或 1,因此可以直接在 if 条件中使用,因为这代表真或假。
如果它是奇数,则 while 循环跳转到下一个 运行,如果它是偶数,则循环的其余部分继续。如果你想切换甚至关闭,请像这样制作 if 语句 if(!($counter % 2))