0

I am using the following code in content.php to repeat an ad insertion. Ads are inserted in the middle of every 2nd row in a grid of thumbnails. There are 3 thumbnails per row.

<?php else : ?> 
 <?php global $wp_query; ?>
 <?php if (($wp_query->current_post + 1) % 5 == 0 ): ?>
  <div class="box">
   <adsense code>
  </div>
<?php endif; ?>

However Google's AdSense policy limits 3 content ads per page and I don't want to violate these terms. How can I limit this to only three instances? Thanks for your help.

3 Answers 3

0

You could just make an easy counter - Untested though.

 <?php else : ?> 
  <?php global $wp_query; ?>
  <?php $ads = 0; ?>
  <?php if ($ads < 3 && (($wp_query->current_post + 1) % 5 == 0 )): ?>
   <div class="box">
    <adsense code>
   </div>
 <?php $ads++; ?>
 <?php endif; ?>
2
  • 1
    Thanks for your answer, but the counter doesn't seem to work, I'm getting the same number of ads returned.
    – Owen
    Commented May 28, 2014 at 12:30
  • Put the $ads = 0; above the loop then. Commented Jun 6, 2014 at 13:57
0

check the post loop counter at the same time as you check the modulus; example:

<?php if (($wp_query->current_post + 1) % 5 == 0 && $wp_query->current_post < 15 ): ?>

0

Here is a function that I use to add a widget area between posts. I can then add whatever widget or adsense or any banner code to that

This goes in your functions.php, no need to alter any other files. For this example, The widget area will be added only after post 3 and 6. You can just set these to match your exact needs

  function widget_after_x_post( $post ) {
    global $wp_query;

    if ( $wp_query->post != $post )
        return;

    if ( 3 != $wp_query->current_post || 6 != $wp_query->current_post)
        return;

    <---ADD YOUR ADSENSE/BANNER/ADD HERE---->
}

 add_action( 'the_post', 'widget_after_x_post' );
1
  • Was any answer helpful. If yes, mark the appropriate one as accepted by clicking on the checkmark just below the up/down arrows. If you problem is still not resolved, please explain what is wrong so that we can help you futher Commented May 29, 2014 at 14:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.