
wordpress如何实现在文章的段落中随机插入百度等广告,下面web建站小编给大家详细介绍一下具体实现代码!
打开主题functions.php文件,写入以下代码:
add_filter( 'the_content', 'insert_ads_randomly' );
function insert_ads_randomly($content){
$pattern = "/<p>.*?</p>/";
//计算文章段落数量
$paragraph_count = preg_match_all($pattern,$content);
//判断文章段落,当前段落必须在5段及以上才插入广告
if($paragraph_count >= 5 && is_single()){
$paragraph_count -=2;
$insert_paragraph=rand(3,$paragraph_count);
$ad_code = '<div>百度广告代码/自己的广告</div>';
return prefix_insert_after_paragraph( $ad_code, $insert_paragraph, $content );
}
return $content;
}
php语法对index.php首页进行判断(根据需要显示不同页面)
wordpress新建页面中页面属性里没有“模板”选项解决方法
标签: wordpress教程, 随机插入广告
上面是“wordpress如何在文章中随机插入百度广告”的全面内容,想了解更多关于 wordpress 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3511.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

dede给dede_sys_enum添加字段father示例
一键抠图APP最新版