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

One more thing... Did you know that people who share AWESOME stuff like this post look awesome too? ;-) Share this on your favourite Facebook group who you think would find this useful and let's reap the benefits together!

Featured On

Inc Magazine Logo  

Sitepoint logo  

CSS Tricks logo   

webdesignerdepot logo   WPMU DEV logo   

and many more!

Where are we hosted?

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

Web Hosting MaxCDN - Speed up your website

New! DIVI 3.1 WordPress Template + PageBuilder

You'll surely create a perfect website with Divi 3.1!

Get an exclusive 10% OFF for CollectiveRay visitors until

Divi 3

 

Monstroid2 (Updated!)

The best-selling template from TemplateMonster has been given an awesome upgrade!

We've got an exclusive 10% OFF for CollectiveRay visitors on Monstroid2 until - use Coupon Code: collectiveray

Monstroid2 Super Update

 

TemplateMonster Bundles

Awesome bundles for awesome websites. $1300 worth of products for $49 only!

We've got an exclusive 10% OFF for CollectiveRay visitors until - use Coupon Code: collectiveray

TemplateMonster Bundles

 

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

 

Advertise on 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.

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   Follow us on Google+   Subscribe to our RSS Feed   Follow us on Twitter