Last active
July 14, 2020 07:20
-
-
Save ma2i/3acbf47cb380033d4be1167a87b74b8c to your computer and use it in GitHub Desktop.
SANGO
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
/* 目次に戻るボタンの表示 */ | |
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