Last active
March 20, 2020 10:59
-
-
Save yhira/ab3d9680ad6413c3c43319c890271fa1 to your computer and use it in GitHub Desktop.
Cocoonの最初のH2見出しから本文最後までをdiv要素で囲む
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
//最初のH2見出しから本文最後までをdiv要素で囲む | |
add_filter('the_content','wrap_h2_1st_to_end', 99999); | |
function wrap_h2_1st_to_end($the_content) { | |
//投稿・固定ページの場合 | |
if (is_singular()) { | |
$h2 = '/<h2/';//H2見出しのパターン | |
if ( preg_match( $h2, $the_content )) {//H2見出しが本文中にあるかどうか | |
$the_content = preg_replace($h2, '<div class="hoge"><h2', $the_content.'</div>', 1);//最初のH2を置換 | |
} | |
} | |
return $the_content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment