You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改WordPress的functions.php代码实现首段及次段后加Adsense广告?

Add AdSense Ads After Both First and Second Paragraphs in WordPress

Hey there! Let's adjust your existing code to drop AdSense ads after both the first and second paragraphs of your WordPress posts. Here's a straightforward, reliable solution that builds on the logic you're already using:

Step-by-Step Modified Code

Replace your current function in functions.php with this updated version:

function insert_ads_after_paragraphs( $content ) {
    // Only apply this to single post pages (skip pages, archives, etc.)
    if ( ! is_single() ) {
        return $content;
    }

    // Paste your actual AdSense ad code here
    $ad_code = '
        <div class="adsense-ad" style="margin: 25px 0; text-align: center;">
            <!-- Replace this with your valid AdSense snippet -->
            <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-XXXXXXXXXXXXXXXX"
                crossorigin="anonymous"></script>
            <ins class="adsbygoogle"
                style="display:block"
                data-ad-client="ca-pub-XXXXXXXXXXXXXXXX"
                data-ad-slot="YYYYYYYYYY"
                data-ad-format="auto"
                data-full-width-responsive="true"></ins>
            <script>
                (adsbygoogle = window.adsbygoogle || []).push({});
            </script>
        </div>
    ';

    // Split the post content into individual paragraphs
    $paragraphs = explode( '</p>', $content );

    // Loop through each paragraph to insert ads
    foreach ( $paragraphs as $index => $paragraph ) {
        // Skip empty paragraphs to avoid extra ads
        if ( trim( $paragraph ) ) {
            $paragraphs[$index] .= '</p>';

            // Insert ad right after the first paragraph
            if ( $index === 0 ) {
                $paragraphs[$index] .= $ad_code;
            }

            // Insert another ad after the second paragraph
            if ( $index === 1 ) {
                $paragraphs[$index] .= $ad_code;
            }
        }
    }

    // Put all the paragraphs (and ads) back together
    return implode( '', $paragraphs );
}
add_filter( 'the_content', 'insert_ads_after_paragraphs' );

Key Details to Note

  • Target Only Single Posts: The is_single() check ensures ads won't show up on pages, category archives, or other non-post content (remove this line if you want ads elsewhere).
  • Customize Your Ad Code: Replace the placeholder AdSense snippet in $ad_code with your actual ad code from Google. I added some inline styles for spacing and centering, but you can tweak or remove those as needed.
  • Avoid Empty Paragraphs: The trim($paragraph) check prevents inserting ads after blank lines that might get counted as paragraphs.
  • Adjust Ad Placement: If you ever want to move ads to different positions (like after 3rd and 5th paragraphs), just change the index numbers (e.g., $index === 2 for the third paragraph).

Quick Testing Tip

After updating your functions.php, clear any WordPress caching plugins and your browser cache to see the changes immediately.

内容的提问来源于stack exchange,提问作者user29891

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:22:31