函数文档

get_archive_template()

💡 云策文档标注

概述

get_archive_template() 函数用于检索当前或父主题中的存档模板文件路径。它基于查询变量动态构建模板层级,并调用 get_query_template() 来获取最终路径。

关键要点

  • 函数返回存档模板文件的完整路径字符串。
  • 模板层级和路径可通过动态钩子 '$type_template_hierarchy' 和 '$type_template' 进行过滤,其中 $type 为 'archive'。
  • 内部逻辑根据查询变量 post_type 生成模板数组,优先匹配特定文章类型的存档模板(如 archive-{$post_type}.php),然后回退到 archive.php。

代码示例

function get_archive_template() {
    $post_types = array_filter( (array) get_query_var( 'post_type' ) );

    $templates = array();

    if ( count( $post_types ) === 1 ) {
        $post_type   = reset( $post_types );
        $templates[] = "archive-{$post_type}.php";
    }
    $templates[] = 'archive.php';

    return get_query_template( 'archive', $templates );
}

📄 原文内容

Retrieves path of 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

Return

string Full path to archive template file.

Source

function get_archive_template() {
	$post_types = array_filter( (array) get_query_var( 'post_type' ) );

	$templates = array();

	if ( count( $post_types ) === 1 ) {
		$post_type   = reset( $post_types );
		$templates[] = "archive-{$post_type}.php";
	}
	$templates[] = 'archive.php';

	return get_query_template( 'archive', $templates );
}

Changelog

Version Description
1.5.0 Introduced.