简体   繁体   中英

Google adsense doesn't appear in a php loop

I am trying to insert google ads between every 5 posts in my wordpress site with infinite scroll.

This is the code I am using to add the google ads between every 5 posts

    <?  

if ( have_posts() ) : $count = 0; while ( have_posts() ) : the_post();

    //before 

    if (($count>1) && ($count%5 == 0) ){ 
    ?>

<!-- AD_HP_RC-2 -->
<div id="adsbetween">
<div id='div-gpt-ad-xxxx-0' style="width:300px; height:450px; display:inline-block;       float:right;">
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-xxxx-0'); });
</script>
</div>
</div>
    <?

    }

    $count++;

The first ad loads fine in the loop but from the second ad, its just the empty space.

I am using dfp in my site so more than 3 google ads are allowed in this case.

This is the site url : http://bluehandwebdesign.com/dev/hk/

Can you please help me on this?

You should give your id unique identifiers. Something like this

   <? if ( have_posts() ) : $count = 0; while ( have_posts() ) : the_post();

    //before 

    if (($count>1) && ($count%5 == 0) ){ 
    ?>

<!-- AD_HP_RC-2 -->
<div id="adsbetween">
<div id='div-gpt-ad-xxxx-<?php echo $count; ?>' style="width:300px; height:450px; display:inline-block;       float:right;">
<script type='text/javascript'>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-xxxx-<?php echo $count; ?>'); });
</script>
</div>
</div>
    <?

    }

    $count++;
  1. Are you also loading the ads in your header? as far as I know the dfp api, every ad you include in the content must be mentioned in the site header as well. In this case, they will have unique identifiers.
  2. Are you delivering AdSense tags through DFP or use AdSense Fallback for DFP? In the first case the max-3-ads rule still applies. In the second case I also wouldn't rely on always having more than 3 ads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM