While troubleshooting with WordPress, sometimes it can be confusing for the developers to determine the exact template files being used to render a particular page in WordPress. In this post, we’ll show you a code snippet that comes in handy for developers at CollectiveRay.com while troubleshooting multipart templates.

Simply add the below code to the functions.php file of your theme

add_action('all','template_snoop');

function template_snoop(){

 $args = func_get_args();

 if( !is_admin() and $args[0] ){

    if( $args[0] == 'template_include' ) {

        echo "<!-- Base Template: {$args[1]} -->\n";

    } elseif( strpos($args[0],'get_template_part_') === 0 ) {

        global $last_template_snoop;

        if( $last_template_snoop )

            echo "\n\n<!-- End Template Part: {$last_template_snoop} -->";

        $tpl = rtrim(join('-',  array_slice($args,1)),'-').'.php';

        echo "\n<!-- Template Part: {$tpl} -->\n\n";

        $last_template_snoop = $tpl;

    }

 }

}

Determining the template file

The above code will show you which template file is being used to render a page by adding an inline comment to the source code of the page you’re viewing on a browser.

To view which template file is being called, simply visit a page and view the source code. That is, you can view the source code by using the shortcut Ctrl+U (Windows, Linux) or Command + U (Mac).

template source code

Do you find this tutorial helpful? Share your thoughts with us...

Download the list of 101 WordPress tricks every blogger should know

101 WordPress tricks

Click here to Download Now

About the Author
David Attard
Author: David AttardWebsite: https://www.linkedin.com/in/dattard/
David has been working in or around the online / digital industry for the last 18 years. He has vast experience in the software and web design industries using WordPress, Joomla and niches surrounding them. As a digital consultant, his focus is on helping businesses get a competitive advantage using a combination of their website and digital platforms available today.

One more thing... Did you know that people who share useful stuff like this post look AWESOME too? ;-)
Please leave a useful comment with your thoughts, then share this on your Facebook group(s) who would find this useful and let's reap the benefits together. Thank you for sharing and being nice!

Disclosure: This page may contain links to external sites for products which we love and wholeheartedly recommend. If you buy products we suggest, we may earn a referral fee. Such fees do not influence our recommendations and we do not accept payments for positive reviews.

Featured On

Inc Magazine Logo  

Sitepoint logo  

CSS Tricks logo   

webdesignerdepot logo   WPMU DEV logo   

and many more!

 

 

Get Started Now With ShutterstockShutterstock

Best Rated Caching Plugin

Make your website faster 

How to make your website FAST!

Step-by-step - free email course, how to make your website load in less than 1 second  

who are we?

CollectiveRay is run by David Attard - working in and around the web design niche for more than 12 years, we provide actionable tips for people who work with and on websites. We also run DronesBuy.net - a website for drone hobbyists.

David attard