问题描述
我有这个循环,它会计数输出:
I have this loop at the same it will count the output:
while ($wp_query->have_posts()) : $wp_query->the_post(); $current++; $current2++;
然后调用正确的html类,我需要这个设计:
Then to call the proper html class I need this for my design:
<div class="span4 <?php if($current == 0) { echo 'first'; } elseif($current == 1) { echo 'second'; } elseif($current == 2) { echo 'first'; } elseif($current == 3) { echo 'second'; } ?>" id="<? echo $current; ?>">
$ count
code> $ count2 from 1.输出应该是这样的:if 0 = first,1 = second,2 = first,3 = second等等。我如何使这个表达式更短,无限数量的 $ count
?我希望我的问题很清楚。
The $count
starts from 0 and $count2
from 1. The output should be like this: if 0=first, 1=second, 2=first, 3=second and so forth. How can I make this expression shorter with unlimited number of $count
? I hope my question is clear. Thank you for those who will help me here.
推荐答案
如果我明白你在这里要求什么,我想你要交替
If I understand what you are asking here, I think you want to alternate your divs with'first' and 'second' classes?
因此,您可以使用%modulo运算子(请参阅)
As such, you can use the % modulo operator (see http://php.net/manual/en/internals2.opcodes.mod.php)
<div class="span4 <?php echo ($current % 2 === 0 ? 'first' : 'second'); ?>" id="<? echo $current; ?>">
这篇关于我如何使这条PHP行更短?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!