返回介绍

locate_template()

发布于 2017-09-11 01:42:27 字数 3356 浏览 1169 评论 0 收藏 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.


description

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

更新日志

Versiondescription
2.7.0Introduced.

相关函数

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文