I'm not sure if this were asked before. I want to obtain a similar effect where the contents and the images are alternated for each post, one time the image is on the left and the text on the right and for the next row the content is displayed with the image on the right and the text on the left. How I can achieve this inside the loop?

1 Answer 1


You should be able to get the current index in loop from the global $wp_query object. With the help of modulo you can then set an alternating css class for a post.

global $wp_query;
while ( have_posts() ) {
  $alignment = ( ($wp_query->current_post + 1) % 2 === 0 ) ? 'even align-right': 'odd align-left';

  // post html markup with class="<?php echo $alignment; ?>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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