You can make use of the global $template
which holds the complete path of the current template being use to display a page. From there you can manipulate the value to get the name of the template
As example, the value of $template
looks like this: (This is on my localhost on my PC)
E:\xammp\htdocs\wordpress/wp-content/themes/pietergoosen2014/page-test.php
To only return the page-test.php
part, I use the following function which uses the PHP functions substr
and strrpos
which finds the last /
character in URL and then returns everything after that:
function get_current_template() {
global $template;
return substr( $template, strrpos( $template, "https://wordpress.stackexchange.com/" ) + 1 );
}
You can then use it anywhere in your template like
echo get_current_template();
I usually just print the template name in my site’s header on my localhost like
add_action( 'wp_head', function ()
{
global $template;
print substr( $template, strrpos( $template, "https://wordpress.stackexchange.com/" ) + 1 );
});
or
add_action( 'wp_head', function ()
{
global $template;
print $template;
});