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?

    I inspect the html and find an identified tag or something unique.

    View the source code and look for the body classes which tell you which template is used. Also gives you the i.d.

    @BradDalton +1. Specially when we are not allowed to install a plugin or write a function to achieve the goal.

  • t31os

    t31os Correct answer

    9 years ago

    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.

    // 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.

    If there is something wrong with this answer, or if anyone could provide comments on what could be done to improve this answer, have at it, drop a comment here and share your thoughts and ideas on how to make it better.

    It didn't work bro, it says "Undefined function"

    @LucasB same here, that's the error I got

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM