How do you find out which template page is serving the current page?

When you activate a wordpress theme, it’s always a hassle to find out which file to go to change things.
Any idea how to simplify things?

But on the other hand, considering the get_template_part functionality, this may be impossible. What do you say?

Solutions Collecting From Web of "How do you find out which template page is serving the current page?"

Hook onto template_include, set a global to note the template set by the theme then read that value back into the footer or header to see which template is being called for a given view.

I spoke about this filter hook before in Get name of the current template file, but go grab a copy of that code and plonk it your theme’s functions.php file.

Then open up the theme’s header.php or footer.php(or wherever you like) and use something like the following to print out the current template.

<div><strong>Current template:</strong> <?php get_current_template( true ); ?></div>

If you wanted to use this on a production site and keep that info away from your non-administrator users, add a little conditional logic.

<?php 
// If the current user can manage options(ie. an admin)
if( current_user_can( 'manage_options' ) ) 
    // Print the saved global 
    printf( '<div><strong>Current template:</strong> %s</div>', get_current_template() ); 
?>

Now you can keep track of what views are using what template, whilst keeping that info away from your visitors.

Well, if all you want is to check which template file has been used to generate the current page then you don’t need to get your hands dirty with code 😉

There’s this handy plugin called Debug Bar. It’s a great helper in many situations including yours. You should definitely check it out – for me and many others it’s a must-have companion for any WP development.

I’ve attached a screenshot that could make you fall in love…

enter image description here

To get the Debug Bar working, you need to enable wp_debug and wp_savequeries options. These options are in disabled state by default.

Before you make any changes though, there are a few points to keep in mind:

  • Do not do it in production environment unless the website doesn’t cater to a lot of traffic.
  • Once you finish debugging, ensure to disable the options (especially the wp_savequeries option since it affects the performance) of the website.

To make the changes:

  1. Open wp_config.php file through a ftp client.
  2. Search for wp_debug option. Edit it to define( 'WP_DEBUG', true );. If the line is not present, add it to the file.
  3. Similarly, edit or add the line define( 'SAVEQUERIES', true ); to the file.
  4. Save. You are ready to debug.

More info: Codex

Add the following code right after the get_header line in each relevant template file:

<!-- <?php echo basename( __FILE__ ); ?> -->

In your browser > view source, and the template name will be displayed as a comment in your html code, e.g.

<!-- page.php -->

I use this handy function that displays the current template only for super admins:

function show_template() {
    if( is_super_admin() ){
        global $template;
        print_r($template);
    } 
}
add_action('wp_footer', 'show_template');

Hope that helps. 🙂

Easiest way I’ve found is to include the WordPress function on the body tag. It’ll add several classes depending on which page you’re viewing (home for the front, page for page, etc).

Check it out here: http://codex.wordpress.org/Function_Reference/body_class

Plus it’s helpful for targeting elements with CSS on those pages.

Getting to know the Template Hierarchy (http://codex.wordpress.org/Template_Hierarchy) as David R mentioned is also a good idea.

There’s another more bare-bones plugin specifically for this purpose. I’m leaning towards installing the debug bar, because those other features look useful, but this one is more basic and specifically for this purpose:
http://wordpress.org/extend/plugins/what-the-file/

One very simple thing I do is to insert an HTML comment identifying the template file in each relevant file of the theme, eg at the top of index.php I have

<!-- index -->

and at the top of front-page.php

<!-- front -->

But obviously that requires modifying the theme. I suspect you could add a custom function in the footer.php file or header.php which would tell you what file was being used. The above method and the reference chart http://codex.wordpress.org/Template_Hierarchy are what I tend to use.

There is a plugin named Theme Check which does exactly this. It displays the name of the current template file in use as a HTML comment.

Here you go:

A HTML-list with all template files in use for the current landing page, including all template-parts from plugins, child theme and/ or parent theme combinations, all in one line of code:

echo '<ul><li>'.implode('</li><li>', str_replace(str_replace('\\', '/', ABSPATH).'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(str_replace('\\', '/', ABSPATH).'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';

You MAY need to check that your server does not returning dubble slashes at any path. Remember to place this after all template files actually been used, like in footer.php, but before admin bar renders.

if admin-bar stuff path is showing at the top, or any other file, change the filename template-loader.php in this line of code to: whatever filname you need to break from. Often: class-wp-admin-bar.php

if you need this in the admin bar, use the right priotity (earliest) to make shure no files are entered at the end of this list. For example:

add_action('admin_bar_menu', 'my_adminbar_template_monitor', -5);

priority -5 make shure it loads first. The key is to call get_included_files() at the right moment, otherwise some array-popping needed!

To break this up:

You can not collect all included template files without PHP backtrace. Superglobals inside template_include wont collect them all. The other way is to “place a marker” in each template file, but if you need to interact with the files first, you hazzle with time and the whole idea.

1) We need to check inside all the files that have been used by current WordPress request. And they are many! Dont be surprised if you are using 300 files before even your functions.php is registered.

$included_files = str_replace('\\', '/', get_included_files());

We are using the PHP native get_included_files(), converting backslashes to forward slashes to match most of WordPress returning paths.

2) We are cutting that array from where the template-loader.php is registered. After that, the populated get_included_files() should only have template files populated.

/* The magic point, we need to find its position in the array */
$path = str_replace('\\', '/', ABSPATH);
$key = $path.'wp-includes/template-loader.php';
$offset = array_search($key, $included_files);

/* Get rid of the magic point itself in the new created array */
$offset = ($offset + 1);
$output = array_slice($included_files, $offset);

3) Shorten down the results, we dont need the path until theme folder or plugin folder, as templates in use, can be mixed from plugins, theme or child theme folders.

$replacement = $path.'wp-content/';
$output = str_replace($replacement, '', $output);

4) Finally, convert from array to a nice HTML list

$output = '<ul><li>'.implode('</li><li>', $output).'</li></ul>';

A last modification might be needed in part3) -replacement, if you dont want required includes by plugins. They might call class-files late, and “intercept” during the template output processing.

However, I found it reasonable to leave them visible, as the idea is to track whats been loaded, even if it is not a “template” that rendering output in this stage.