除最后一篇 WordPress 文章外,每两篇文章执行一次操作
我正在使用以下内容为每两篇文章中的幻灯片创建一张幻灯片。
<?php $i = 0; $slideshow_query = new WP_Query("showposts=6&cat=10"); ?>
<div id="slideshow-posts">
<div class="newslide">
<?php while ($slideshow_query->have_posts()) : $slideshow_query->the_post(); $do_not_duplicate = $post->ID; $i++; ?>
//my content
<?php if ($i == 2) { ?></div> <div class="newslide"><?php $i = 0; } ?>
<?php endwhile; ?>
</div>
问题是它在最后创建一个空白幻灯片 - 除了最后一次出现偶数帖子之外,我如何让它每两个帖子(或每个偶数帖子)创建一个新闻幻灯片?
谢谢
I'm using the following to create a slide for a slideshow out of every two posts.
<?php $i = 0; $slideshow_query = new WP_Query("showposts=6&cat=10"); ?>
<div id="slideshow-posts">
<div class="newslide">
<?php while ($slideshow_query->have_posts()) : $slideshow_query->the_post(); $do_not_duplicate = $post->ID; $i++; ?>
//my content
<?php if ($i == 2) { ?></div> <div class="newslide"><?php $i = 0; } ?>
<?php endwhile; ?>
</div>
Problem is it creates a blank slide at the end - how would I get it to carry out creation of a news slide every two posts (or every even post) except for the last time an even post occurs?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在寻找的运算符称为模或取模,并返回除法的余数:
当与除数 2 一起使用时,该运算符可以方便地识别循环中的偶数/奇数行:
或者在您的情况下是这样的:
为了查明您是否已到达最后一篇文章,您可以将
$i + 1
(因为我们从 0 开始计数)与项目的全部数量进行比较由您的查询返回(您的查询中最多 6 个) 例子)。如果匹配,则关闭。
更新
我根据我的评论扩展了上面的示例。这没有经过测试,但我相信这就是我记得它工作的方式(即帖子计数)。
The operator you're likely looking for is called modulus or modulo and returns your the remainder of a division:
This operator comes in handy to identify even/odd rows in a loop when used with a divisor of 2:
Or in your case something like this:
In order to find out of you have reached the last post, you could compare
$i + 1
(since we start counting from 0) with the full amount of items returned by your query (max 6 in your example). And then close the<div>
if they match.UPDATE
I've extended the example above according to my comment. This isn't tested, but I believe that's how I remember it to work (the post count that is).