Created
October 24, 2019 21:56
-
-
Save 09045392431/a52c06a182887fb3c2c498eb59832b60 to your computer and use it in GitHub Desktop.
goggle_adsense2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function add_ad_before_h2_for_3times($the_content) { | |
//広告を自動挿入する | |
$ad = <<< EOF | |
////////////////////////////////// | |
//ここに広告コード&アドセンスコード// | |
////////////////////////////////// | |
EOF; | |
if ( is_single() ) { | |
$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($h2s[0][0], $ad.$h2s[0][0], $the_content); | |
} | |
if ( $h2s[0][2] ) {//3番目のH2見出し手前に広告を挿入 | |
$the_content = str_replace($h2s[0][2], $ad.$h2s[0][2], $the_content); | |
} | |
} | |
} | |
} | |
return $the_content; | |
} | |
add_filter('the_content','add_ad_before_h2_for_3times'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment