Skip to main content
5 of 7
added 80 characters in body
kaiser
  • 50.8k
  • 27
  • 150
  • 244

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

<?php
! defined( 'ABSPATH' ) AND exit;
/** Plugin Name: Get Template file name */
function wpse10537_get_template_name( $file )
{
    static $name;
    if ( 'template_include' === current_filter() )
    {
            $name = 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 $name;
}
add_action( 'template_include', 'wpse10537_get_template_name' );

This plugin can run as MU-Plugin.

You can then simply call wpse10537_get_template_name() at any point (in for example a theme template). This avoids cluttering the global namespace.

kaiser
  • 50.8k
  • 27
  • 150
  • 244