
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,我们会在看到邮件的第一时间内为您处理!

 DEDECMS输出生成页面显示白板解决办法
 酷宣AI官网:一个专注于帮助用户快速生成高颜值文章的智能工具
 一款将知识内容转化为数字化形式的一站式知识付费平台——海豚知道