WordPress: How to Get Template Name?

2014-10-11 WordPress

It may happen that for some particular reasons you will have to know the exact template name, that you’re currently using.

Below code works in functions.php and obviously in other places as well.

global $post;
$tpl_name = get_post_meta($post->ID, '_wp_page_template', true);

Simple function can look like this:

function get_current_template_name()
{
  global $post;
  $tpl = get_post_meta($post->ID, '_wp_page_template', true);
  return $tpl;
}

Call this function inside any template and var_dump result to see what you will get.