Wordpress - 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


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. :)

Tags:

Templates