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

时间:2014-08-02 14:10:28

标签: adsense

如何在一个网站上拥有多个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 个答案:

答案 0 :(得分:69)

要在一个页面上放置多个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({});
});

答案 1 :(得分:12)

使用jQuery ...

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

答案 2 :(得分:2)

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

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

<!-- 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>

答案 3 :(得分:-4)

如果您想在一个页面上使用多个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中不允许这样做。 (可能完全没必要。您只需粘贴三个广告代码段,或者 - 相同的代码段三次。)

相关问题