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

    query posts
      if posts exist
        then begin the loop
          if post is even: <h1>title</h1><p>content</p>
          if post is odd: <div>its image</div>

this is what i’m trying to get, a different output for odd/even posts: for even posts we will show the title and the content while for odd posts we will show its image (the thumbnail, for example).
How to get this result?

I query post in this way

query_posts('category_name=category-name');

then i don’t know how to continue

3 s
3

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