Applying Even/Odd Classes
<div class="example-class<?php echo ($xyz++%2); ?>">
Used inside a loop, class names would be named .example-class0 and .example-class1 alternating. Increasing the "2" number allows you to increment in thirds or fourths or whatever you need:
class="class<?php echo ($xyz++%3); ?>"
Classes: class0 class1 class2 class0 class1 class2
Change number to 4, this is the result: class0 class1 class2 class3 class0 class1 class2 class3
FYI, the 2nd example should read:
class="class"doh:
class="class\"ok I give up. Just add 3 instead of 2 :P
Easiest method I have ever seen, thank you!
That was easy!
Thanks~!
Wow. That rocks! Thank you so much!
thanku :) heaps :)
It´s very Good
Awesome,
The easiest one so far. So we do not have to use the old
one…. Saves time :)
what about this technique, i think its much faster then taking decision on if statement.
$class = ($class==’class1′) ? “class2″ : “class1″ ;
for($i=1; $i<=10; $i++){
if($i%2){ echo "class"; } else { echo "class"}
}
Simple example can be use in any loop even in wordpress.
And… if you php.ini has enabled the directive “short open tag” then the snippet becomes shortest ever!! hehe