locate_template
Retrieve the name of the highest priority template file that exists.
Description
(string) locate_template( (string) $template_names, (constant) $load = false, (bool) $require_once = true );
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.
Returns (string)
The template filename if one is located.
Parameters (3)
- 0. $template_names (string)
- Template file(s) to search for, in order.
- 1. $load — Optional. (constant) =>
false
- If true the template file will be loaded if it is found.
- 2. $require_once — Optional. (bool) =>
true
- Whether to require_once or require. Default true. Has no effect if
$load
is false.
Usage
if ( !function_exists( 'locate_template' ) ) { require_once ABSPATH . WPINC . '/template.php'; } // Template file(s) to search for, in order. $template_names = ''; // If true the template file will be loaded if it is found. $load = false; // Whether to require_once or require. Default true. Has no effect if $load is false. $require_once = true; // NOTICE! Understand what this does before running. $result = locate_template($template_names, $load, $require_once);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/template.php
- function locate_template($template_names, $load = false, $require_once = true ) {
- $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;
- break;
- }
- }
- if ( $load && '' != $located )
- load_template( $located, $require_once );
- return $located;
- }