how to get a different html for odd/even posts?

You don’t need a new variable for counting posts, WordPress has one already in $wp_query->current_post.

<?php while (have_posts()): the_post() ?>
    <?php if ($wp_query->current_post % 2 == 0): ?>
        even
    <?php else: ?>
        odd
    <?php endif ?>
<?php endwhile ?>

If you use a custom WP_Query instance as iEmanuele suggested then it will be $query->current_post instead.

Leave a Comment