How can I see what template parts are being called for rendering the viewable page?

You can view the template part by using get_template_part($slug)
It’s one of those hidden gems inside of WordPress that don’t get the attention they deserve.
The get_template_part function is essentially a PHP include or require, on steroids:

It already knows where your theme is located and it will look for the requested file in that theme’s directory

It doesn’t issue a warning or fatal out if the requested file does not exist

It can search for other suitable files, if the requested one is not found

It knows about child themes and parent themes

Long story short, the get_template_part function allows you to break your theme down into smaller templates (or template parts), which can be reused across your other templates.

For example:

get_template_part( 'navigation', get_post_type() );

Where get_post_type() will return the name of the post type that is currently shown, so if we’re on a Post, it’ll attempt to load navigation-post.php and fallback to navigation.php. If we’re on a Page, navigation-page.php and navigation.php. If we’re looking at a custom post type, say a Book, it will look for navigation-book.php and fall back to navigation.php.

The real power of get_template_part comes from a function called locate_template, which does the whole searching in parent theme and child theme folders, and the reverting to other templates in a stack. The get_template_part function simply builds an array of templates for locate_template to look for. Here’s a quick example:

get_template_part( 'one', 'two' );

Creates an array of “one-two.php” and “one.php” (in that specific order) and passes it on to locate_template, which then loops through that array and looks for the files in the child and parent themes directories. The order is really important here, it’s kind of why file names have priority over their locations (parent theme or child theme) and explains the reason behind the lookup sequence.

It’s also worth noting, that functions such as get_header, get_sidebar and get_footer are very similar to get_template_part with a sort of hard-coded first argument.

get_template_part is located in wp-includes/general-template.php and locate_template is in wp-includes/template.php.

Leave a Comment