0

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?

0

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() ) {
  the_post(); 
  $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, privacy policy and cookie policy

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