Even / Odd within Foreach Loop

<ul>
  <?php $n = 0; foreach($page->children() as $child): $n++; ?>
  <li class="<?php echo ($n%2) ? 'odd' : 'even' ?>">
    ...
  </li>
  <?php endforeach ?>
</ul>

You can change “odd” and “even” to whatever names you need.  Sourced here.

Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.