Your condition should be written like this:
if (is_page_template('path/file.php')) {
// Do stuff
}
I believe the confusion is a result of two things:
- The docs refer to “name” ambiguously. Specifying “file name” would make the documentation much more clear.
- The code behind
is_page_template()
shows theget_page_template_slug()
function at its core. This function actually returns a file name, not the template slug. https://codex.wordpress.org/Function_Reference/get_page_template_slug
When specifying an argument for the is_page_template()
function (as in the example above), the file path is relative to the theme root.
This function will not work inside the loop.
EDIT: an important issue to note here as well. The is_page_template()
function will return empty/false if the page is using the default template from the hierarchy. If a custom template is not assigned, you must use another method, such as basename(get_page_template())
. See Jacob’s answer here for more details: https://wordpress.stackexchange.com/a/328427/45202