0

I made a custom taxonomy called - work-category and a template file for it called - taxonomy-work-category.php. That part work just fine.

For a purpose I did put get_page_template() in that taxonomy-work-category.php file to get the current template file name. But it does print out as page.php (with full path ofcourse). I was expecting it to print out taxonomy-work-category.php.

Even when I'm certain it is printing out from a custom taxonomy template file - Why doesn't get_page_template() function doesn't returns the taxonomy template file name?

1

get_page_template returns the template of the current active post/page. You're not on a post/page though, you're in a listing/archive.

When you get page.php what you're seeing is the template of the first post/page. The taxonomy listing itself is not a page, it's a listing, and so it makes no sense for it to have a page template, as it is not a page.

See here for an answer that should do what you want:

Get name of the current template file

this may also work:

/* show me what the body class will look like */
echo body_class() . "\n";

/* make sure i've got $template */
global $template;

/* print the active template path and filename */
print_r($template);

( Although why check if you're in template X inside template X, surely the answer will always be yes? )

  • There is a good use case here that I came across. I am dynamically pulling in templates and assigning individual stylesheets with a sass/gulp workflow. I am registering and enqueueing these stylesheets via functions.php, but am unable to pull the active template since there is technically no post, all of the information is being rendered via an api call that is triggered by the get request. With this method, I was able to check the template being used and enqueue the appropriate stylesheet without loading unnecessary resources i.e. one massive stylesheet for the whole site. – Mark T Broomell Oct 11 '17 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.