WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am modifying the theme TwentyEleven.

In index.php there is the loop which accesses content.php template:

<?php while ( have_posts() ) : the_post(); ?>

  <?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; ?>

The trouble is that I need different formatting (different classes) for odd and even posts. How to do this?

My only idea is to remove content.php and insert the code directly to index.php with variable $i counting the position in the loop (by ++$i). But that's bad.

share|improve this question
This isn't really a WordPress question. You can use the nth-child CSS selector, or toggle a class outpout variable in PHP. – anu Aug 6 '12 at 8:45
@anu But it can be done nicely with wp core functions. – kaiser Sep 26 '12 at 11:56

Open up content.php and replace this:

<?php post_class(); ?>

...with this:

<?php post_class( ++$GLOBALS['wp_query']->wpse60971_current_post % 2 === 0 ? 'even' : 'odd' ); ?>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.