I've found this to display the current name of the file used in template:
function get_template_name () {
foreach ( debug_backtrace() as $called_file ) {
foreach ( $called_file as $index ) {
if ( !is_array($index[0]) AND strstr($index[0],'/themes/') AND !strstr($index[0],'footer.php') ) {
$template_file = $index[0] ;
}
}
}
$template_contents = file_get_contents($template_file) ;
preg_match_all("Template Name:(.*)\n)siU",$template_contents,$template_name);
$template_name = trim($template_name[1][0]);
if ( !$template_name ) { $template_name = '(default)' ; }
$template_file = array_pop(explode('/themes/', basename($template_file)));
return $template_file . ' > '. $template_name ;
}
Source: get name of page template on a page
It works quite well, except that in the backend, in the template select box, I get this ugly extra entry:
Does anybody have any idea how to fix it? I don't even know why this function is called in the backend. Is there a conditional function like is_frontend()
- maybe this would solve the problem?
functions.php
as a bug, I will agree completely with your premise. To make matters worse I scanned the WordPress core code and found about 5 places where there could have been a hook to allow you to handle this issue yet I found none. I'd suggest posting a ticket on core.trac.wordpress.org.template_include
hook, which t31os suggested, solve the issue? Or maybe I've misunderstood you.