I’ve found this to display the current name of the file used in template:
function get_template_name () {
foreach ( debug_backtrace() as $called_file ) {
foreach ( $called_file as $index ) {
if ( !is_array($index[0]) AND strstr($index[0],'/themes/') AND !strstr($index[0],'footer.php') ) {
$template_file = $index[0] ;
}
}
}
$template_contents = file_get_contents($template_file) ;
preg_match_all("Template Name:(.*)n)siU",$template_contents,$template_name);
$template_name = trim($template_name[1][0]);
if ( !$template_name ) { $template_name = '(default)' ; }
$template_file = array_pop(explode('/themes/', basename($template_file)));
return $template_file . ' > '. $template_name ;
}
Source: get name of page template on a page
It works quite well, except that in the backend, in the template select box, I get this ugly extra entry:
Does anybody have any idea how to fix it? I don’t even know why this function is called in the backend. Is there a conditional function like is_frontend()
– maybe this would solve the problem?
You could set a global variable during the
template_include
filter and then later check that global vairable to see which template has been included.You naturally wouldn’t want the complete path along with the file, so i’d recommend truncating down to the filename using PHP’s
basename
function.Example code:
Two functions, one to set the global, one to call upon it.
You can then call upon
get_current_template
wherever you need it in the theme files, noting this naturally needs to occur after thetemplate_include
action has fired(you won’t need to worry about this if the call is made inside a template file).For page templates there is
is_page_template()
, bearing in mind that will only help in the case of page templates(a far less catch all function).Information on functions used or referenced above:
apparently this is enough:
or just use it directly in template (I tend to echo in footer.php in HTML comment)
Between native WP functions like get_template_part() and PHP’s native includes the most reliable way to see theme’s files used is to fetch list of all included files and filter out whatever doesn’t belong to theme (or themes when parent and child combination is used):
An addition (more sweet code) to other answers here.
Template Name
To just get the current page template name, use the following line.
File Name
When you just want to echo the current template file name, go with the following
Edit: Here’s the new version of the plugin wrapped up in a class. It shows both the current template file name, as well as the template hierarchy file name in the shutdown hook at the most bottom of the page.
What the plugin tells you:
Just copy the following code into a file and name it
wpse10537_template_info.php
, upload it to your plugins directory and activate it.This plugin can run as MU-Plugin too.
You can then simply call
wpse10537_get_template_name()
at any point (in for example a theme template). This avoids cluttering the global namespace.The template name is stored in the postmeta table, so all you need to do is put this somewhere in your loop:
This doesn’t address all of the OP’s question, but the code below is certainly more elegant than regular expressions and parsing the template file itself.
If you’re on a Page that is using a Page Template, and you want to get the page template’s Name (ie: the human-readable name that you defined in the comments at the top of your template PHP file), you can use this little nugget:
I wanted to get the template name because I was really sick of the silly-long-ass class names that the built-in WordPress
body_class
function creates when you’re using a template. Luckily there’s a filter hook at the very end of that function to let you append your own class names as well. Here’s my filter. Hope someone finds it useful:This filter will take whatever you named your page template, replace spaces with dashes and make everything lower case so it looks like all the other WordPress classes.
There’s an issue with the
preg_match_all
line. Try this instead:Also, you can use
if (!is_admin()) { .... }
to run things on the frontend only.Play along with:
Written at:
How do you find out which template page is serving the current page?
if
admin-bar stuff
path is showing at the top, or any other file, change the filenametemplate-loader.php
in this line of code to: whatever filname you need to break from.if you need this in the admin bar, use the right priotity (earliest) to make shure no files are entered at the end of this list. For example:
priority
-5
make shure it loads first. The key is to render this line at the right moment.It does not returning the “current” template-file, but all the current in use for the current page-load. Maybe “cut out” with some logic from that idea.
The
get_included_files()
“last” key is the last registered included file, propably the last template-file/ -part used in the footer by sidebar widget or something. Propably, cos mutiple included files does not re-register/ populates again in get_included_files().Otherwise, the intension must be clear to hack this problem. There is no way for a included file to report itself as included, until it has been included. Then its propably to late to use the scenario.
Most of the “time” you would like:
But thats not possible if the template is loaded outside WordPress core method of
get_template_part
. Re-design your needs instead! Maybeloop_start()
,in_the_loop()
andadd_action('the_post')
has the solution you want, to alter data depending of template thats gonna load for each entry within a loop.