function add_ad_before_h2_for_3times($the_content) { //広告(AdSense)タグを記入 $ad = <<< EOF ////////////////////////// //ここにアドセンスを挿入// ////////////////////////// EOF; if ( is_single() ) {//投稿ページ $h2 = '/^<h2.*?>.+?<\/h2>$/im';//H2見出しのパターン if ( preg_match_all( $h2, $the_content, $h2s )) {//H2見出しが本文中にあるかどうか if ( $h2s[0] ) {//チェックは不要と思うけど一応 if ( $h2s[0][0] ) {//1番目のH2見出し手前に広告を挿入 $the_content = str_replace(
![本文記事1、2、3番目のH2見出し手前にアドセンスを挿入するWordPressカスタマイズ方法](https://cdn-ak-scissors.b.st-hatena.com/image/square/de4fd63fa0b40f2084b40e60a343a16de7124162/height=288;version=1;width=512/https%3A%2F%2Fnelog.jp%2Fwp-content%2Fuploads%2F2015%2F07%2F6054732942_b8de71b6f2_b1.jpg)