locate_template()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
locate_template( string|array $template_names, bool $load = false, bool $load_once = true, array $args = array() ): string
Описание
Ищет в каталоге таблицы стилей (stylesheet) перед каталогом шаблона (template) и wp-includes/theme-compat, чтобы темы, наследующие от родительской темы, могли переопределить всего один файл.
Оригинал (английский)
Searches in the stylesheet directory before the template directory and wp-includes/theme-compat so that themes which inherit from a parent theme can just overload one file.
Параметры
$template_names
string|array
обязательный
$load
bool
необязательный
= false
$load_once
bool
необязательный
= true
$args
array
необязательный
= array()
Возвращаемое значение
string
Исходный код
wp-includes/template.php:722
function locate_template( $template_names, $load = false, $load_once = true, $args = array() ) {
global $wp_stylesheet_path, $wp_template_path;
if ( ! isset( $wp_stylesheet_path ) || ! isset( $wp_template_path ) ) {
wp_set_template_globals();
}
$is_child_theme = is_child_theme();
$located = '';
foreach ( (array) $template_names as $template_name ) {
if ( ! $template_name ) {
continue;
}
if ( file_exists( $wp_stylesheet_path . '/' . $template_name ) ) {
$located = $wp_stylesheet_path . '/' . $template_name;
break;
} elseif ( $is_child_theme && file_exists( $wp_template_path . '/' . $template_name ) ) {
$located = $wp_template_path . '/' . $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, $load_once, $args );
}
return $located;
}
История изменений
| Версия | Описание |
|---|---|
| 5.5.0 | The $args parameter was added. |
| 2.7.0 | Introduced. |

