10/24/2017

WordPress 文章列表中插入 AdSense 廣告

WordPress 的文章列表多數由佈景主題中的 index.php、category.php、tag.php 分別為首頁、分類、標籤等列表掛鉤

經由 while 迴圈(while loop)調用文章

所以只要將 AdSense 代碼加入下面迴圈中間
<?php while ( have_posts() ) : the_post(); ?>
<?php if ($wp_query->current_post == 3 ) : ?>
<style type="text/css">
.adslot_1 { margin-bottom: 2rem; }
</style>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle adslot_1"
     style="display:block"
     data-ad-format="fluid"
     data-ad-layout-key="-xx+xx+xx-xx-xx"
     data-ad-client="ca-pub-1234"
     data-ad-slot="5678"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php endif; ?>
<?php get_template_part( 'content', get_post_format() ); ?>

$wp_query->current_post == 3 從內置循環計數器變量中取 3 插入 AdSense 廣告

.adslot_1 { margin-bottom: 2rem; } 幫 AdSense 廣告設定下邊界距離

<ins class="adsbygoogle adslot_1" 將 style 設定加入 class

沒有留言:

張貼留言