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


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

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 

Monstrous B-Day Party

CLICK HERE for discounts!

Monstrous bday party

How to make your website FAST!

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


The Outstanding HungryJPEG Bundles

AWESOMENESS! Bundles of premium font + graphic packs at more than 96% OFF!  Get a bundle for just $9 - ONLY!

The Hungry JPEG Awesome font bundles


Work with CollectiveRay.com

CollectiveRay (formerly known as DART Creations) is interested in developing partnerships with mutual benefit. If you like the stuff we publish and would like to develop a relationship, we'd be happy to hear from you. Go on - drop us a line - we'd love to hear from you :-)


Disclosure: CollectiveRay is funded personally out of pure passion for helping people working with websites. We do however generate some income through recommendations of products. This means if you click on a link and purchase an item we link to, we will receive a small sum out of that sale. We usually partner with vendors to make your purchase cheaper than buying direct.


InMotion Hosting CollectiveRay Deal

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

Follow us on Social

Follow us on Facebook   Subscribe to our RSS Feed   Follow us on Twitter


Where are we hosted?

This site is proudly powered by FAST VPS InMotion Servers and given an insane speed thanks to StackPath CDN!

Web Hosting stackpath