1

When I say 'Every 3rd Iteration' I mean 'Every 3rd Iteration... starting from number 4'

If it were just every 3rd, I would target the appropriate iterations like so:

<?php if ($count % 3 == 0) : ?>

Bear in mind I've set $count to 1 beforehand

But the iterations need to start with the 4th, then 7th, 10th etc.

Does anyone know how I can acheive this? Can I do it with a simple if statement like the one above?

Unfortunately I don't think a for loop will be possible as this is a WordPress loop I'm dealing with

2
  • 1
    <?php $offset=1; if (( $count - $offset) % 3 == 0) : ?> Commented May 18, 2014 at 18:16
  • <?php if ($count % 3 == 0 && $count != 3) : ?> Commented May 18, 2014 at 18:18

2 Answers 2

4

Your thoughts about using the modulus operator are sound, you just need to expand the scope of your logic surrounding it:

You want every third iteration after the initial 4 have passed. Begin your logic after the first 4:

if ($count > 4)

Then, you want each 3 after that. Your counter includes the initial 4 iterations you didn't want to include, so remove that from your counter and check if the current iteration is a multiple of 3:

if (($count - 4) % 3 === 0)

This should give you what you're looking for.

if ($count > 4)
{
    if (($count - 4) % 3 === 0)
    {
        ...
    }
}

You can also put this into one line:

if ($count > 4 && ($count - 4) % 3 === 0)
{
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks! Never would have been able to think of that myself haha. I've got the following inside my WP Loop but it doesn't seem to be working properly unfortunately <?php if ($count > 4 && ($count - 4) % 3 === 0) : ?><h1>Break</h1><?php endif; ?>
You can see it here, the heading 'Break' should appear after the first 3 boxes but instead it appears after the first 6? bit.ly/SrEl4h
...actually never mind, I've just realised it's much easier to achieve it with jQuery instead! Thanks for the help anyway
2
foreach($array as $key=>$value){
    if($key % 3 != 0){
        continue;
    }
}

Comments

Your Answer

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