Alternate custom content in the loop

You could initialize a second counter variable that increments when your first counter variable increments, and use that with modulus to alternate your code blocks.

First initialize two counter variables:

<?php if (have_posts()) : $show_alternate = 1; $which_alternate = 1; while (have_posts()) : the_post(); ?>

Then in your .pin-alternate-content output code you could do something like this:

<?php if($show_alternate++ % 3 == 0) : ?>
    <div class="slide pin-alternate-content">

    <?php if($which_alternate++ % 2 == 1) : ?>
        <h3>Number one</h3>
        <h4>Heading one</h4>
        <small>Small text one</small>
    <?php else : ?>
        <h3>Number two</h3>
        <h4>Heading two</h4>
        <small>Small text two</small>
    <?php endif; ?>

    </div>
<?php endif; ?>

I’ve tested this code and after three slides, it will display the Number one content, and after the next three slides, the Number two alternate content displays, and after another three slides the Number one content shows again, and it will continue alternating like this for however many sets of three posts you have.

Leave a Comment