function ads_before_headline_v2($the_content) {
global $post;
// 投稿ページで広告が有効なときのみ実行
if ( is_single() && !get_post_meta( $post->ID, 'disable_ads', true )) {
//
タグを抽出
$h2_pattern = '/.+?<\/h2>/im';
if ( preg_match_all( $h2_pattern, $the_content, $matches ) ) {
$ad1 = get_option( 'insert_articles_ads1' );
$ad2 = get_option( 'insert_articles_ads2' );
// 広告位置と設定を配列で管理してループ処理
for ( $i = 1; $i <= 4; $i++ ) {
if ( isset( $matches[0][$i] ) ) {
if ( get_option( 'insert_articles_ads1_' . ( $i + 1 ) ) ) {
$the_content = str_replace( $matches[0][$i], $ad1 . $matches[0][$i], $the_content );
}
if ( get_option( 'insert_articles_ads2_' . ( $i + 1 ) ) ) {
$the_content = str_replace( $matches[0][$i], $ad2 . $matches[0][$i], $the_content );
}
}
}
}
}
return $the_content;
}
add_filter('the_content','ads_before_headline_v2');
制作実績 | あさみの似顔絵オンライン