tags:

views:

82

answers:

3

I am looking to create alternating designs for the content of each post returned in my loop. In short I want the first post to display left align, next right align, and so on. I have not been able to find a way to do this. Any ideas?

+1  A: 

Try something like this:

$count = 0;
foreach ($posts as $post) {
    echo "<div class=\"" . (++$count % 2 ? 'left' : 'right') . "\">"
        . $post['postText'] // or whatever the crazy wordpress thing is
        . "</div>"
    ;
}
nickf
A: 

You could loop through the results and then check if an incremented counter is even or odd and display left or right depending on that.

Chaim Chaikin
A: 

Have a look at the modulo operator '%'

0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
...
100 % 2 = 0
101 % 2 = 1

You can have a repeating pattern of as many as you like:

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
....

C.

symcbean