locate_template() - WordPress 函数参考中文文档

返回介绍

locate_template()

发布于 2017-09-11 字数3358 浏览 973 评论 0

locate_template( string|array $template_names,  bool $load = false,  bool $require_once = true )

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


描述

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.


参数

$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 value: false

$require_once

(bool)
(Optional)
Whether to require_once or require. Has no effect if $load is false.

Default value: true


返回值

(string) The template filename if one is located.


源代码

File: 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;
		} 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 );

	return $located;
}

更新日志

Version 描述
2.7.0 Introduced.

相关函数

Uses

  • wp-includes/template.php:
    load_template()

Used By

  • wp-includes/general-template.php:
    get_header()
  • wp-includes/general-template.php:
    get_footer()
  • wp-includes/general-template.php:
    get_sidebar()
  • wp-includes/general-template.php:
    get_template_part()
  • wp-includes/general-template.php:
    get_search_form()
  • wp-includes/template.php:
    get_query_template()
  • Show 1 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example
    Load a specific template part based on the current pagename.

    
    if (locate_template('content-' . $pageName . '.php') != '') {
    	// yep, load the page template
    	get_template_part('content', $pageName);
    } else {
    	// nope, load the content
    	the_content();
    }
    
    

上一篇:locale_stylesheet()

下一篇:login_footer()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!