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?
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?
Share Improve this question asked Jan 16, 2020 at 9:54 sialfasialfa 32910 silver badges29 bronze badges1 Answer
Reset to default 0You 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; ?>"
}