首页 文章

如何在一个页面上使用多个adsense单位?

提问于
浏览
23

你如何在一个网站上有多个adsense单位? Google提供的唯一代码是每个单元 .

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:250px"
     data-ad-client="ca-pub-123456"
     data-ad-slot="123456"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

如果我想在一个网站上使用多个adsense单位怎么办?我只使用 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>(adsbygoogle = window.adsbygoogle || []).push({}); 一次,然后将 <ins ...></ins> 代码放在我想要的位置 .

问题是只解析并显示了第一个adsense单元 . 您需要做什么才能显示多个adsense单元?

这是我使用它的方式(仅显示第一个 ins ):

<!doctype html>
<html>
    <body>
        <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="first"></ins>

         <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="second"></ins>

         <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="third"></ins>

        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
        <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
    </body>
</html>

4 回答

  • -3

    要在一个页面上放置多个adsense单元,您必须添加更多行 (adsbygoogle = window.adsbygoogle || []).push({}); .

    因此,如果您有3个广告单元,则需要使用3次 .

    (adsbygoogle = window.adsbygoogle || []).push({});
    (adsbygoogle = window.adsbygoogle || []).push({});
    (adsbygoogle = window.adsbygoogle || []).push({});
    

    如果要动态执行此操作,请使用以下命令:

    [].forEach.call(document.querySelectorAll('.adsbygoogle'), function(){
        (adsbygoogle = window.adsbygoogle || []).push({});
    });
    
  • 64

    使用jQuery ...

    $(".adsbygoogle").each(function () { (adsbygoogle = window.adsbygoogle || []).push({}); });
    
  • 1

    只需在页面底部( </body> 之前)调用 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 一次 .

    接下来,将广告代码分开放置,如下所示:

    <!-- Top Banner Ad -->
    <ins class="adsbygoogle"
        style="display:inline-block;width:320px;height:100px"
        data-ad-client="ca-pub-1234567890"
        data-ad-slot="4693644638"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    
    <!-- Responsive Ad -->
    <ins class="adsbygoogle"
        style="display:block"
        data-ad-client="ca-pub-1234567890"
        data-ad-slot="3097818646"
        data-ad-format="auto"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    
  • 11

    如果您想在一个页面上使用多个AdSense单元,则需要创建并粘贴多个AdSense代码段:

    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="first"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="second"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
         style="display:inline-block;width:300px;height:250px"
         data-ad-client="ca-pub-123456"
         data-ad-slot="third"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>
    

    AdSense中仅允许有限数量的代码修改 . https://support.google.com/adsense/answer/1354736?hl=en

    我可能会回答为什么“只有第一个adsense单元被解析并显示”,我可以尝试向您展示如何修改您的示例以显示所有三个广告,但在我看来这是无关紧要的(在这种情况下),因为它是AdSense中不允许使用 . (可能完全没必要 . 您只需粘贴三个广告代码段,或者 - 相同的代码段三次 . )

相关问题