An addition (more sweet code) to other answers here.
Template Name
To just get the current page template name, use the following line.
is_page() AND print get_page_template_slug( get_queried_object_id() );
File Name
When you just want to echo the current template file name, go with the following
Edit: Here's the new version of the plugin wrapped up in a class. It shows both the current template file name, as well as the template hierarchy file name in the shutdown hook at the most bottom of the page.
What the plugin tells you:
- Is the template from the parent of child/current theme?
- Is the template served from a subfolder? If yes: Tells you the name
- The template file name.
Just copy the following code into a file and name it wpse10537_template_info.php, upload it to your plugins directory and activate it.
<?php
/** Plugin Name: (#10537) »kaiser« Get Template file name */
if ( ! class_exists( 'wpse10537_template_name' ) )
{
add_action( 'plugins_loaded', array( 'wpse10537_template_name', 'init' ) );
class wpse10537_template_name
{
protected static $instance;
public $stack;
public static function init()
{
is_null( self :: $instance ) AND self :: $instance = new self;
return self :: $instance;
}
public function __construct()
{
if ( is_admin() )
return;
add_action( 'wp', array( $this, 'is_parent_template' ), 0 );
add_action( 'wp', array( $this, 'get_template_file' ) );
add_action( 'template_include', array( $this, 'get_template_name' ) );
add_action( 'shutdown', array( $this, 'get_template_name' ) );
}
public function get_template_name( $file )
{
if ( 'template_include' === current_filter() )
{
$this->to_stack(
"Template file"
,basename( $file )
);
return $file;
}
// Return static var on echo call outside of filter
if (
current_user_can( 'manage_options' )
AND defined( 'WP_DEBUG' )
AND WP_DEBUG
)
return print implode( " – ", $this->stack );
}
public function get_template_file()
{
if ( ! is_post_type_hierarchical( get_post_type() ) )
return;
$slug = get_page_template_slug( get_queried_object_id() );
if ( ! strstr( $slug, "/" ) )
return $this->to_stack( "Template", $slug );
$this->to_stack(
"Subdirectory"
,strstr( $slug, "/", true )
);
$this->to_stack(
"Template (in subdirectory)"
,str_replace( "/", "", strstr( $slug, "/" ) )
);
}
public function is_parent_template()
{
if ( ! is_null( wp_get_theme()->parent ) )
return $this->to_stack( 'from parent theme' );
$this->to_stack( 'from current/child theme' );
}
public function to_stack( $part, $item = '' )
{
$this->stack[] = "{$part}: {$item}";
}
} // END Class wpse10537_template_name
} // endif;
This plugin can run as MU-Plugin too.
You can then simply call wpse10537_get_template_name() at any point (in for example a theme template). This avoids cluttering the global namespace.
functions.phpas 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. – MikeSchinkel♦ Feb 26 '11 at 20:13template_includehook, which t31os suggested, solve the issue? Or maybe I've misunderstood you. – chodorowicz Feb 27 '11 at 15:29functions.phpas page template file) still persists. I'll try to post it on trac, haven't done it yet. Greetings! – chodorowicz Feb 27 '11 at 16:44