Php 在WordPress页面的第二段添加广告

Php 在WordPress页面的第二段添加广告,php,wordpress,Php,Wordpress,我在functions.php文件中使用以下代码,将谷歌广告添加到每篇文章的第二段。就目前情况而言,该准则有效,但仅适用于帖子。我希望有人在这个论坛上可以帮助调整代码,使这个功能适用于网页以及帖子 //Insert ads after second paragraph of single post content. add_filter( 'the_content', 'prefix_insert_post_ads' ); function prefix_insert_post_ads( $

我在functions.php文件中使用以下代码,将谷歌广告添加到每篇文章的第二段。就目前情况而言,该准则有效,但仅适用于帖子。我希望有人在这个论坛上可以帮助调整代码,使这个功能适用于网页以及帖子

//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {

    $ad_code = '<div>AD Code to go here</div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

    return $content;
}

// Parent Function that makes the magic happen

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 );
}
//在单篇文章内容的第二段之后插入广告。
添加过滤器(“内容”、“前缀插入”、“发布广告”);
函数前缀\插入\发布\广告($content){
$ad_code='ad code to go here';
如果(is_single()&&!is_admin()){
返回前缀\在\段后插入\段($ad\ U代码,2,$content);
}
返回$content;
}
//使魔法发生的父函数
函数前缀\u insert\u在\u段落之后($insert,$paragration\u id,$content){
$closing_p='

'; $段落=分解($closing\u p,$content); foreach($index=>$paration的段落){ 如果(删减($段)){ $段落[$index]。=$closing\u p; } 如果($paration_id==$index+1){ $段落[$索引]。=$插入; } } 返回内爆(“”,$段); }
而不是
是单一的()
,它只测试是否显示单一的帖子,你应该使用,
是单一的()
是页面()
是附件()

,只要你有内容就可以,我肯定你的页面上有这些内容。谢谢你的帮助!