其实就是判断查找文章的第一个p,然后,插入代码,放到functions里使用即可。

function themeInit($archive) {

   // 判断是否是文章,如果是就插入广告
   $ad_code = '<div>这是你的广告</div>';
   if ($archive->is('single')) {
        $archive->content = prefix_insert_after_paragraph( $ad_code, 2, $archive->content );;
    }
}

// 插入广告所需的功能代码
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 );
}