# locate_template()

URL: https://chugunov.pro/api-wordpress/functions/locate_template/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.7.0.

## Сигнатура

```php
locate_template( string|array $template_names, bool $load = false, bool $load_once = true, array $args = array() ): string
```

## Описание

Ищет в каталоге таблицы стилей (stylesheet) перед каталогом шаблона (template) и wp-includes/theme-compat, чтобы темы, наследующие от родительской темы, могли переопределить всего один файл.

## Параметры

- `$template_names` `string|array` — обязательный. Файл(ы) шаблона для поиска, по порядку.
- `$load` `bool` — необязательный, по умолчанию `false`. Если true, найденный файл шаблона будет загружен.
- `$load_once` `bool` — необязательный, по умолчанию `true`. Использовать require_once или require. Не имеет эффекта, если $load равно false.
- `$args` `array` — необязательный, по умолчанию `array()`. Дополнительные аргументы, передаваемые в шаблон.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/template.php:722`

```php
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.

## Связанные

Использует: [`wp_set_template_globals`](https://chugunov.pro/api-wordpress/functions/wp_set_template_globals/), [`is_child_theme`](https://chugunov.pro/api-wordpress/functions/is_child_theme/), [`load_template`](https://chugunov.pro/api-wordpress/functions/load_template/).
Используется в: [`get_header`](https://chugunov.pro/api-wordpress/functions/get_header/), [`get_footer`](https://chugunov.pro/api-wordpress/functions/get_footer/), [`get_sidebar`](https://chugunov.pro/api-wordpress/functions/get_sidebar/), [`get_template_part`](https://chugunov.pro/api-wordpress/functions/get_template_part/), [`get_search_form`](https://chugunov.pro/api-wordpress/functions/get_search_form/), [`get_query_template`](https://chugunov.pro/api-wordpress/functions/get_query_template/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/locate_template/
