get_author_template()
云策文档标注
概述
get_author_template() 函数用于检索作者模板文件的路径,遵循特定的模板层级规则。它基于当前查询的作者对象动态生成模板文件名,并返回完整路径。
关键要点
- 函数返回作者模板文件的完整路径字符串。
- 模板层级顺序为:author-{nicename}.php、author-{id}.php、author.php,例如 author-john.php、author-1.php、author.php。
- 可通过动态钩子 '$type_template_hierarchy' 和 '$type_template'(其中 $type 为 'author')过滤模板层级和路径。
- 内部使用 get_queried_object() 获取作者对象,并调用 get_query_template() 检索模板。
代码示例
function get_author_template() {
$author = get_queried_object();
$templates = array();
if ( $author instanceof WP_User ) {
$templates[] = "author-{$author->user_nicename}.php";
$templates[] = "author-{$author->ID}.php";
}
$templates[] = 'author.php';
return get_query_template( 'author', $templates );
}
原文内容
Retrieves path of author template in current or parent template.
Description
The hierarchy for this template looks like:
- author-{nicename}.php
- author-{id}.php
- author.php
An example of this is:
- author-john.php
- author-1.php
- author.php
The template hierarchy and template path are filterable via the ‘$type_template_hierarchy’ and ‘$type_template’ dynamic hooks, where $type is ‘author’.
See also
Source
function get_author_template() {
$author = get_queried_object();
$templates = array();
if ( $author instanceof WP_User ) {
$templates[] = "author-{$author->user_nicename}.php";
$templates[] = "author-{$author->ID}.php";
}
$templates[] = 'author.php';
return get_query_template( 'author', $templates );
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |