How to find out which WordPress template is being used in a Theme

How do you quickly find out which template for a page or post in your WordPress theme is being used without systematically going through each template?

WordPress uses a structure of templates formed in a hierarchy of importance, when one isn’t there it falls back to the next in the chain.

wp-template-hierarchy

Click for a closer look

If you want to quickly see what template you are using in an archive,  post/page or other post type you can add a function into your functions.php file in the theme which will display the path and template name at the foot of each page.

add_action( 'admin_bar_menu', 'show_template' );
function show_template() {
global $template;
print_r( $template );
}

Once the function is added and you’re still logged in as admin, have a look at a page in the front end and scroll down to the footer, the path to the template being used is output at the bottom of the page – this is only visible to logged in admins via the dashboard.

wordpress-template-name-output

 

When you are done you can simply comment out the add_action line by adding 2 forward slashes and then uncomment it when you need to use it again to see template usage.

//add_action( 'admin_bar_menu', 'show_template' );

If you prefer to use a plugin, there is a plugin for this here which I made which shows the template name at the bottom of the screen and also has a WordPress link to the template hierarchy for more reference.

1 Comment

  1. Dare on September 5, 2016 at 7:33 pm

    Thank you for this post. Saved me from tons of aggravation today. I found several posts but didn’t have the confidence to use them until I found this post. Worked as intended.

Leave a Comment





%d bloggers like this: