You’ve just installed a new theme. And you love it! However, there is one piece of text you don’t like. Or perhaps a graphic in a certain portion turns you off. “No problem.” you say to yourself. “This is WordPress – I can just modify the correct theme files”.
Except that it’s not easy to find out which file is generating what. Those days are gone.
Modern WordPress pages are “stitched” together from a large number of components. There are a lot of outdated tutorials on the Internet still giving advice to modify a particular WordPress theme file if you want to make changes. These can still work with certain themes, or ones that are out of date. But for the past several years, WordPress has become a lot more dynamic and compartmentalized.
In this tutorial, I’m going to show you which template is used in WordPress. Not just one template however. I’m going to show you how to get all the relevant files that go into making an individual WordPress page.
The Problem: Looking at “single.php” Isn’t Enough
Let’s say that the part of the theme you want to change is displayed when viewing an individual post. Experienced WordPress admins will know that the “single.php” file is responsible for displaying it.
If you don’t know the basic template for the page that you’re viewing, here’s some simple code that will show you the underlying template:
function display_template() { global $template; echo "Template File Name " . $template; } add_action( 'wp_footer', 'display_template');
This will generate output like this at the bottom of every page:
You can see in the screenshot above, that it’s “single.php” which is stored in the global “$template” variable. Great! So you navigate to your WordPress dashboard, click “Appearance->Editor”, and go to your theme’s “single.php” file.
Unfortunately, this is what you see:
There’s hardly any code inside the box! What happened to the main code of the WordPress loop? What happened to the code showing the meta information? Where is everything??
The answer is simple: single.php is not the only WordPress theme file that is displaying content. WordPress pages now call a bunch of other PHP files and “stitch” together content. What we want is a way to force WordPress to display all the theme files that are being used.
And here’s how to do just that.
The Solution: Getting a Complete List of WordPress Template Files
Instead of the above code showing just a basic template file, use the following code instead. If you don’t know how to pate custom code snippets into WordPress like this, here’s a step by step tutorial on how to do just that.
function display_template() { $included_files = get_included_files(); $stylesheet_dir = str_replace( '\\', '/', get_stylesheet_directory() ); $template_dir = str_replace( '\\', '/', get_template_directory() ); foreach ( $included_files as $key => $path ) { $path = str_replace( '\\', '/', $path ); if ( false === strpos( $path, $stylesheet_dir ) && false === strpos( $path, $template_dir ) ) unset( $included_files[$key] ); } echo "Complete list of included files: \n"; echo str_replace(array('<?php ','?>'), '', highlight_string( '<?php ' . var_export($included_files, true) . ' ?>', true ) ); } add_action( 'wp_footer', 'display_template');
Now when you load a page, you get the following output:
As you can see, this is very different from the previous screenshot. I bet you had no idea that so many files were being used to generate just a simple WordPress post! Note that not only is “single.php” being used, another file called “content-single.php” is also contributing to the page.
This list won’t tell you which of the template files are generating a specific output, but it’s a great starting point. You can narrow down the list based on an educated guess, and finally be able to pinpoint what you need to change.
So far, this is the most comprehensive way I’ve found to display the list of files that go into a single WordPress post. Using it, you can now modify your theme in the right place to get the look you need!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply