Locate_template () is used to retrieve the highest-priority template files that exist, as well as to load the template files directly.
When a locate_template () function retrieves a child theme, it takes precedence over the template of the child theme and no further retrieves the parent theme.
Usage
Locate_template ($template _names, $load, $require _once);
Parameters
$template _names
(array) (required) the template file name to be introduced (requires an extension) to match the file one by one, and the higher the precedence.
Default value: None
$load
(Boolean) (optional) If set to True, the template file is introduced directly.
Default value: False
$require _once
(Boolean) (optional) If set to True if it has been introduced this time no longer introduced (require_once), it will be introduced (require) whether or not it is introduced.
(This parameter takes effect only if the $load is True).
Default value: False
return value
(string) Returns the path of a specified template file if it exists, otherwise an empty string is returned.
Example
if (locate_template (' content-') $pageName. '. php ')!== {
//exists, introducing template file
Get_template_part (' content ', $pageName);
} else{
//does not exist, direct display content
the_content ();
}
Other
This function is located at: wp-includes/template.php
Quick Retrieve templates
Get_query_template () is used to quickly retrieve a page template, and it needs to be based on a predetermined page type (type).
It differs from locate_template () in that the type of page needs to be filled out, and the {$type}_template template path filter is generated.
Usage
Get_query_template ($type, $templates);
Parameters
$type
(string) (must) the type of page of the template file to be obtained, and the file name (such as single) that does not have an extension for the template file.
Default value: None
$templates
(array) (optional) a list of alternative templates.
Default value: Empty array
return value
Returns the path to the template file.
Example
If present, introduce a 404-page Template:
if ('!= get_404_template ()) include (Get_404_template ());
Other
This function is located at: wp-includes/template.php