The What
It’s been available for you since version 2.7.0.
Syntax
string locate_template( array $template_filenames, bool $load = false, bool $require_once = true );
Description
This construct scans the child theme first and then the parent theme for the template filename. If found and selected, it loads the file using load_template
. Then it returns the template it found, if any. The intent of this function is to provide the means for:
- A child theme to override the template file within the parent.
- Automatically locate the appropriate template file.
- Automatically load the found template file, when selected.
Parameters
template_filenames
- An array of template filenames, with the highest priority one as the first element.
load
- optional Set to
true
to autoload the template file if found. The default value isfalse
. require_once
- optional When set to
true
, a PHPrequire_once
will be used to autoload the file; else a PHPrequire
is used.
Return Values
If found, this function returns the template filename; else an empty string is returned.
You will grow in this profession when you incrementally and systematically stretch yourself....bit-by-bit.
Real World ExamplesBasic
Let’s look at three different themes in order to see how to use locate_template
.
Go Deep into CorePro
Let’s go into WordPress Core, walk through it line-by-line to understand how it works, how it scans the child theme first and then the parent, and what it returns.
PHP Constructs
This function uses the following PHP constructs: