Last active
October 5, 2018 11:33
-
-
Save goripom4/667af6b4cde15ada79f6693467538d86 to your computer and use it in GitHub Desktop.
特定のカスタム投稿タイプを除いてH2見出し手前にアドセンスを挿入する(変更後)
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
<?php | |
// ↓ここからコピペ↓// | |
function add_ad_before_h2_for_2times($the_content) { | |
//1つ目の広告タグを挿入 | |
$ad1 = <<< EOF | |
////////////////////////// | |
//ここにアドセンスを挿入// | |
////////////////////////// | |
EOF; | |
//2つ目の広告タグを挿入 | |
$ad2 = <<< EOF | |
////////////////////////// | |
//ここにアドセンスを挿入// | |
////////////////////////// | |
EOF; | |
if ( is_singular( 'カスタム投稿タイプのスラッグ' ) ) { | |
} else { | |
$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($h2s[0][0], $ad1.$h2s[0][0], $the_content); | |
} | |
if ( $h2s[0][1] ) {//2番目のH2見出し手前に広告を挿入 | |
$the_content = str_replace($h2s[0][1], $ad2.$h2s[0][1], $the_content); | |
} | |
} | |
} | |
} | |
return $the_content; | |
} | |
add_filter('the_content','add_ad_before_h2_for_2times'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment