[How to] Find Which Page Template Is Used By WordPress
Templating is a great feature of WordPress blogging system. If you want to build a CMS system using WordPress, you will be using lots of them. You can use different templates for each category, tag, page, etc. But sometimes this gets confusing. In this post I will give you a simple snippet to find out which template is used by WordPress easily.
Template selection in WordPress is done using current query context. If user is viewing a category page, wordpress searches for category-category_name.php
, if not found it uses category.php
. And if you don’t have a category.php
, it will default to archive.php
. This sometimes gets confusing and it is hard to tell which template is used. Using simple snippet below, you can easily see which template WordPress uses in your footer. Just put it into your functions.php
file and it will show you current template name.
add_action('wp_footer', 'shailan_show_template_name'); function shailan_show_template_name() { global $template; if( current_user_can('administrator') ){ echo "<div style='text-align:center;font-size:smaller'>"; print_r("To serve this page <strong>" . basename($template) . "</strong> is used."); echo "</div>"; } }
I wrapped the code to check for administrator privileges, so it won’t display your template path to your visitors.
Update: Alternatively, you can also make the output an HTML comment so that it won’t be visible on the front end but only on the source code of a page. See it in the example below:
add_action('wp_footer', 'ms_template_name_comment'); function ms_template_name_comment() { global $template; if( current_user_can('administrator') ){ echo "<!-- Template: " . basename($template) . " -->"; } }
I hope you liked this tip. Enjoy!