wpseek.com
Bazujące na WordPress narzędzie wyszukiwania dla deweloperów i twórców motywów.



locate_template › WordPress Function

Od2.7.0
Przestarzałyn/a
locate_template ( $template_names, $load = false, $load_once = true, $args = array() )
Parametry: (4)
  • (string|array) $template_names Template file(s) to search for, in order.
    Wymagane: Tak
  • (bool) $load If true the template file will be loaded if it is found.
    Wymagane: Nie
    Domyślny: false
  • (bool) $load_once Whether to require_once or require. Has no effect if `$load` is false. Default true.
    Wymagane: Nie
    Domyślny: true
  • (array) $args Optional. Additional arguments passed to the template. Default empty array.
    Wymagane: Nie
    Domyślny: array()
Powrót:
  • (string) The template filename if one is located.
Zdefiniowane na:
Codex:
Dziennik zmian:
  • 5.5.0

Retrieves the name of the highest priority template file that exists.

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.


Źródło

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;
}