get_post_type_archive_template()
云策文档标注
概述
get_post_type_archive_template() 函数用于检索当前或父模板中文章类型存档模板的路径。它基于查询变量和文章类型对象来确定是否返回存档模板路径。
关键要点
- 函数返回文章类型存档模板文件的完整路径字符串,若无存档则返回空字符串。
- 通过 get_query_var('post_type') 获取查询变量,并处理数组情况。
- 使用 get_post_type_object() 检查文章类型对象是否存在且支持存档。
- 模板层次结构和路径可通过动态 Hook(如 '$type_template_hierarchy' 和 '$type_template')进行过滤。
- 相关函数包括 get_archive_template()、get_query_var() 和 get_post_type_object()。
代码示例
function get_post_type_archive_template() {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$obj = get_post_type_object( $post_type );
if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
return '';
}
return get_archive_template();
}注意事项
- 函数自 WordPress 3.7.0 版本引入。
- 确保文章类型对象具有 has_archive 属性,否则可能返回空路径。
原文内容
Retrieves path of post type archive template in current or parent template.
Description
The template hierarchy and template path are filterable via the ‘$type_template_hierarchy’ and ‘$type_template’ dynamic hooks, where $type is ‘archive’.
See also
Source
function get_post_type_archive_template() {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$obj = get_post_type_object( $post_type );
if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
return '';
}
return get_archive_template();
}
Changelog
| Version | Description |
|---|---|
| 3.7.0 | Introduced. |