• Hello,

    I searched using ‘counting loops’ but didn’t come across anything pertinent. I am trying to construct my page so that every 3 or 4 posts, I can insert a Google Adsense section, how do I achieve this?

    Thank you,
    Richard

Viewing 4 replies - 1 through 4 (of 4 total)
  • Just stick a variable (let’s say $post_count) with the increment operator into The Loop, and check on the value of that. So locate:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    in your template, and add $post_count++ to it, like so:

    <?php if (have_posts()) : while (have_posts()) : the_post(); $post_count++; ?>

    Then in The Loop where the ad is to display:

    <?php if(4 == $post_count) : ?>

    ~ Your adsense code goes here ~

    <?php endif; $post_count = 0; ?>

    Thread Starter jbbrwcky

    (@jbbrwcky)

    Ahhh! thank you 🙂

    Thread Starter jbbrwcky

    (@jbbrwcky)

    I found a bug with that, the $post_count = 0; needs to be before the endif;

    Thanks, it worked perfectly!

    Hmm, yeah. Guess I was distracted by an upgrade or something…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Counting Loops’ is closed to new replies.