On this page
locate_template( string|array $template_names, bool $load = false, bool $require_once = true, array $args = array() ): string
Retrieves the name of the highest priority template file that exists.
Description
Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat so that themes which inherit from a parent theme can just overload one file.
Parameters
$template_namesstring|array Required-
Template file(s) to search for, in order.
$loadbool Optional-
If true the template file will be loaded if it is found.
Default:
false $require_oncebool Optional-
Whether to require_once or require. Has no effect if
$loadis false.
Default:
true $argsarray Optional-
Additional arguments passed to the template.
Default:
array()
Return
string The template filename if one is located.
Source
File: wp-includes/template.php. View all references
function locate_template( $template_names, $load = false, $require_once = true, $args = array() ) {
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
$located = STYLESHEETPATH . '/' . $template_name;
break;
} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
$located = TEMPLATEPATH . '/' . $template_name;
break;
} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
break;
}
}
if ( $load && '' !== $located ) {
load_template( $located, $require_once, $args );
}
return $located;
}
Related
Uses
| Uses | Description |
|---|---|
| load_template() wp-includes/template.php | Requires the template file with WordPress environment. |
Used By
| Used By | Description |
|---|---|
| get_header() wp-includes/general-template.php | Loads header template. |
| get_footer() wp-includes/general-template.php | Loads footer template. |
| get_sidebar() wp-includes/general-template.php | Loads sidebar template. |
| get_template_part() wp-includes/general-template.php | Loads a template part into a template. |
| get_search_form() wp-includes/general-template.php | Displays search form. |
| get_query_template() wp-includes/template.php | Retrieves path to a template. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/locate_template