Skip to content

Instantly share code, notes, and snippets.

@gatespace
Last active July 30, 2022 22:04
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gatespace/4482529 to your computer and use it in GitHub Desktop.
Save gatespace/4482529 to your computer and use it in GitHub Desktop.
WordPressのテーマにおいて、テンプレート階層に基づきどのテンプレートファイルが使われているか書き出すコード。 WordPressがデバッグモードのときのみ表示します。 使っているテンプレートファイルはグローバル変数 $template に保存されています。 (ただし、header.php や get_template_part などでインクルードされているファイルを除く) wp_footerにフックする形で関数が実行されますので、テーマのfunctions.phpの適当な箇所に追記してください。
/*
WordPressのテーマにおいて、テンプレート階層に基づきどのテンプレートファイルが使われているか書き出すコード。
ただし、header.php や get_template_part などでインクルードされているファイルを除く。
*/
add_action('wp_footer', 'view_template_files');
if ( !function_exists( 'view_template_files' ) ):
function view_template_files() {
if ( defined('WP_DEBUG') && WP_DEBUG ) {
global $template;
$template_name = basename( $template, '.php' );
$template_dir = basename ( dirname( $template ) );
$style_top = ( is_admin_bar_showing() ) ? "35px" : "0px";
echo '<code style="position: fixed; top: ' . $style_top . '; right: 10px; z-index: 9999; background-color: rgba(255, 255, 255, 0.5); padding: 10px; color: #000000; border: solid 2px #000000; ">';
echo "テーマのディレクトリ名:" . $template_dir;
echo " テンプレートファイル名:" . $template_name;
echo "</code>\n";
}
}
endif;
@gatespace
Copy link
Author

@wokamoto さん。ありがとうございます。反映しました。
なんちゃってコピペプログラマーなもんで(汗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment