WordPress.org

Forums

Counting Loops (5 posts)

  1. jbbrwcky
    Member
    Posted 10 years ago #

    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

  2. Kafkaesqui

    Posted 10 years ago #

    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; ?>

  3. jbbrwcky
    Member
    Posted 10 years ago #

    Ahhh! thank you :)

  4. jbbrwcky
    Member
    Posted 10 years ago #

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

    Thanks, it worked perfectly!

  5. Kafkaesqui

    Posted 10 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags