Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ma2i/3acbf47cb380033d4be1167a87b74b8c to your computer and use it in GitHub Desktop.
Save ma2i/3acbf47cb380033d4be1167a87b74b8c to your computer and use it in GitHub Desktop.
SANGO
/* 目次に戻るボタンの表示 */
add_filter('the_content', function( $content ){
if ( is_singular() && strpos( $content, ' id="ez-toc-container' ) !== false ){
//挿入するHTML
$back_toc = '<div class="back-toc" style="font-weight:bold;font-size:15px;"><a href="#ez-toc-container"><i class="fa fa-arrow-up" aria-hidden="true"></i> 目次に戻る</a></div>';
$count = 0;
$content = preg_replace_callback('/<h2 *[^>]*>/i', function( $m ) use (&$count, $back_toc) {
$count++;
if( $count == 1 ) {
return $m[0]; //一番最初のh2には挿入しない
} else {
return $back_toc.PHP_EOL.$m[0];
}
}, $content, -1).$back_toc.PHP_EOL;
}
return $content;
}, 99999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment