函数原型
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.
是否弃用
未弃用
函数参数
-
$template_names
string|array
Required - Template file(s) to search for, in order.
-
$load
bool
Optional - If true the template file will be loaded if it is found.
Default:
false -
$require_once
bool
Optional - Whether to require_once or require. Has no effect if
$loadis false.
Default:
true -
$args
array
Optional - Additional arguments passed to the template.
Default:
array()
函数返回值
string The template filename if one is located.
函数位置
File: wp-includes/template.php.
函数源码
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;
}
源码链接
变更日志
| Version | Description |
|---|---|
| 5.5.0 | The $args parameter was added. |
| 2.7.0 | Introduced. |

