wordpress在文章中插入广告代码

付费节点推荐


免费节点


节点使用教程


//在文章内容的第二段后面插入广告
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
//这里添加广告代码
$ad_code = "


";
if ( is_single() && ! is_admin() ) {
// 修改5 这个段落数
return prefix_insert_after_paragraph( $ad_code, 5, $content );
}
return $content;
}

// 插入广告所需的功能代码
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
$closing_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 );
}

未经允许不得转载:Bcoder资源网 » wordpress在文章中插入广告代码

相关推荐

更多优质资源关注微信公众号: bcoder

bcoder
赞 (2)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册