Created
July 3, 2023 19:02
-
-
Save mopeneko/7be345cb17392712c9a477b1de1a3964 to your computer and use it in GitHub Desktop.
[THE THOR] h2の上に広告コードを挿入する(1記事目はスキップ)
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 | |
// h2の上に広告コードを挿入する | |
function insert_adcode($the_content) { | |
// 固定ページではそのまま表示 | |
if (!is_single()) { | |
return $the_content; | |
} | |
$has_h2 = preg_match_all('/^<h2.*?>.+?<\/h2>$/im', $the_content, $h2_list, PREG_SET_ORDER); | |
// h2タグが無ければそのまま表示 | |
if (!$has_h2) { | |
return $the_content; | |
} | |
foreach ($h2_list as $idx => $h2) { | |
// 1記事目はスキップ | |
if ($idx === 0) continue; | |
$the_content = str_replace($h2[0], '[adcode]' . $h2[0], $the_content); | |
} | |
return $the_content; | |
} | |
add_filter('the_content', 'insert_adcode'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment