Home Forums Add-ons Repeater Field Even / Odd Display


Even / Odd Display

  • I want to display every other row differently. On the odd numbered ones I want the image to display on the right and on the even rows I want the image to display on the left. Heres what I have currently that displays all the rows the same:

        if( have_rows('magazines') ): ?>
            <? while( have_rows('magazines') ): the_row();
                // vars
                $name = get_sub_field('magazine_name');
                $image = get_sub_field('magazine_image');
                $content = get_sub_field('magazine_description');
                $link = get_sub_field('magazine_link');
                $background = get_sub_field('magazine_background');
                <div class="fullscreen background parallax" style="background-image:url('<?php echo $background['url']; ?>');" data-img-width="1600" data-img-height="1064" data-diff="100">
                    <div class="content-a">
                        <div class="content-b">
                            <div class="wrap">
                                <div class="two-thirds first">
                                    <h1><?php echo $name;?></h1>
                                    <p><?php echo $content;?></p>
                                    <a class="button" href="<?php echo $link;?>">View Magazine</a>
                                <div class="one-third">
                                    <img data-name="image" src="<?php echo $image[url];?>" class="magazine-cover" alt="">
            <?php endwhile; ?>
        <?php endif;
  • Hi @chriswathen

    You can use the get_row_index() function to check the current row. It should be something like this:

    while( have_rows('magazines') ): the_row();
        if( get_row_index() % 2 == 0 ){
            // this is an even row
            // code to display the image on the left
        } else{
            // this is an odd row
            // code to display the image on the right

    I hope this helps 🙂

  • Thank you very much, this is exactly what I was looking for.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Even / Odd Display’ is closed to new replies.