在文章内容中加入 Google AdSense / 谷歌广告的方法

在模板函数 (functions.php)中加入以下代码

1 在文章内容的第二段后出现 Google AdSense / 谷歌广告

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
    $ad_code ='<div align="center">
<style>
.adsbygoogle-comment { width: 99%; height: 60px; padding: 1rem 0px }
@media(min-width: 500px) { .adsbygoogle-comment { width: 99%; height: 70px; padding: 1rem 0px} }
@media(min-width: 800px) { .adsbygoogle-comment { width: 99%; height: 70px; padding: 1rem 0px} }
</style> 
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 广告单元5 -->
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-3422805437689454"
     data-ad-slot="8030259996"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>'; 
    if ( is_single() && ! is_admin() ) {
    // 下面一行数字2代表段落
    return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }
    return $content;
}

function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
    $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
    if ( trim( $paragraph ) ) {
    $paragraphs[$index] .= $closing_p;
    }
    if ( $paragraph_id == $index + 1 ) {
    $paragraphs[$index] .= $insertion;
    }
    }
    return implode( '', $paragraphs );
}

2 在文章内容中随机出现 Google AdSense / 谷歌广告

add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads($content){
    $pattern = "/<p>.*?<\/p>/";
    $paragraph_count = preg_match_all($pattern,$content); //计算文章的段落数量
    if($paragraph_count >= 1 && is_single()){//如果文章的段落数量少于8段,则不会插入文章段落广告
    $paragraph_count -=2;
    $insert_paragraph=rand(3,$paragraph_count);
    $ad_code ='<div align="center">
<style>
.adsbygoogle-comment { width: 99%; height: 60px; padding: 1rem 0px }
@media(min-width: 500px) { .adsbygoogle-comment { width: 99%; height: 70px; padding: 1rem 0px} }
@media(min-width: 800px) { .adsbygoogle-comment { width: 99%; height: 70px; padding: 1rem 0px} }
</style> 
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 广告单元5 -->
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-3422805437689454"
     data-ad-slot="8030259996"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>'; 
    return prefix_insert_after_paragraph( $ad_code, $insert_paragraph, $content );
    }
    return $content;
} 
    
// 插入广告所需的功能代码
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
         $closing_p = '</p>';
    $paragraphs = explode( $closing_p, $content );
    foreach ($paragraphs as $index => $paragraph) {
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= $closing_p;
        }
        if ( $paragraph_id == $index + 1 ) {
            $paragraphs[$index] .= $insertion;
        }
    }
    return implode( '', $paragraphs );
}