locate_template()
locate_template( string|array $template_names, bool $lo…
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)
在TEMPLATEPATH和wp includes/theme compat之前的STYLESHEETPATH中搜索,以便从父主题继承的主题可以重载一个文件。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $template_names | (string | array) | 必需 | 按顺序搜索模板文件。 |
| $load | (bool) | 可选 | 如果为true,则会加载找到的模板文件。 |
| $require_once | (bool) | 可选 | 是要求一次还是要求。如果$load为false,则不起作用。 |
返回(Return)
(string)模板文件名(如果有)。
源码(Source)
/**
* Retrieve the name of the highest priority template file that exists.
*
* Searches in the STYLESHEETPATH before TEMPLATEPATH so that themes which
* inherit from a parent theme can just overload one file.
*
* @since 2.7.0
*
* @param string|array $template_names Template file(s) to search for, in order.
* @param bool $load If true the template file will be loaded if it is found.
* @param bool $require_once Whether to require_once or require. Default true. Has no effect if $load is false.
* @return string The template filename if one is located.
*/
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;
}
}
if ( $load && '' != $located )
load_template( $located, $require_once );
return $located;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 2.7.0 | wp-includes/template.php:653 | 6 | 1 function |
笔记(Notes)
请注意,locate_template()不能防止目录遍历攻击,因此,如果要将用户提供的模板名称传递给函数,请确保它来自三个适当的位置之一(active theme directory、parent theme directory或/wp includes/theme compat/directory)。
例子
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!