{ Which template am I on? }

This is an issue I run into once in a while when trudging through someone else’s WP theme code. It may be needlessly complex or make use of an unfamiliar framework, and you’re not quite sure which template you need to find. Usually a reference to the file can be found in the body classes, but you can’t always count on body_class() being implemented.

Take a quick jaunt into the theme functions.php file and add this action for a quick reference:

add_action('wp_head', function() {
   global $template;
   var_dump($template);
});

If for whatever reason you are still using PHP 5.2x (you shouldn’t be), decouple the anonymous function and create a named one for the action:

add_action('wp_head', 'debug_wp_head');
function debug_wp_head() {
   global $template;
   var_dump($template);
}

This will literally dump the path to the template being used to render the current page / post including the file name. $template comes from wp-includes/template-loader.php, check out the source here starting at line ~44.

You can also use the get_template_page_slug() function, which is used in several places in wp-includes/post-template.php. It returns just the file name of the current template, more specifically the _wp_page_template meta field value for it. It accepts a post ID or post object (WP_Post) but defaults to the $post global.

add_action('wp_head', function() {
   var_dump(get_page_template_slug());
});

NOTE: Up to WP 4.7.0 this only worked for pages, now it will work with any post type.