PHP syntax for if / elseif / else but for 4 conditions

I have a list of 29 posts, after the 1st i want to insert an advert (ad_unit 1), after the 9th i want to insert an advert (ad_unit 2), after the 18th i want to insert an advert (ad_unit 3) and after the 27th i want to insert an advert (ad_unit 4). I have come up with this code below but i can only display ad_unit 1 after 1st post then the rest of positions i get ad_unit 2. Thanks for your help

$t[’__loop’] = $t[’__loop’] + 1;

<?php if ($t['__loop'] == 1) : ?>
<?= get_option('ad_unit_1'); ?>
<?php elseif ($t['__loop'] === 10) : $t['__loop'] = 1; ?>
<?= get_option('ad_unit_2'); ?>
<?php endif; ?>

Can you post your entire loop code? That will make it easier to see what’s going on and how to help you.

    $ad = 0;
    for($i=1; $i<=29; $i++) {
       echo "post#".$i."<br>";
       if (($i % 9)==0) {
          echo "ad Number: " . $ad . "<br>";

Something loosely like this would work. You want to throw out a post every 9 items, just use modulo and keep count somehow. I used $i as a post counter and $ad as an advert counter. Simple enough…

Sponsor our Newsletter | Privacy Policy | Terms of Service