Skip to content

Instantly share code, notes, and snippets.

@yhira
Last active March 20, 2020 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhira/ab3d9680ad6413c3c43319c890271fa1 to your computer and use it in GitHub Desktop.
Save yhira/ab3d9680ad6413c3c43319c890271fa1 to your computer and use it in GitHub Desktop.
Cocoonの最初のH2見出しから本文最後までをdiv要素で囲む
//最初の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