get_embed_template()
云策文档标注
概述
get_embed_template() 函数用于检索嵌入模板文件的路径,遵循特定的模板层级结构。它基于查询对象的 post_type 和 post_format 动态构建模板列表,并返回匹配的模板文件路径。
关键要点
- 函数返回嵌入模板文件的完整路径,遵循层级:embed-{post_type}-{post_format}.php、embed-{post_type}.php、embed.php。
- 模板层级和路径可通过动态钩子 '$type_template_hierarchy' 和 '$type_template' 进行过滤,其中 $type 为 'embed'。
- 内部使用 get_queried_object() 获取查询对象,并基于 post_type 和 post_format 构建模板数组。
- 最终调用 get_query_template() 来检索并返回模板路径。
代码示例
function get_embed_template() {
$object = get_queried_object();
$templates = array();
if ( ! empty( $object->post_type ) ) {
$post_format = get_post_format( $object );
if ( $post_format ) {
$templates[] = "embed-{$object->post_type}-{$post_format}.php";
}
$templates[] = "embed-{$object->post_type}.php";
}
$templates[] = 'embed.php';
return get_query_template( 'embed', $templates );
}
原文内容
Retrieves an embed template path in the current or parent template.
Description
The hierarchy for this template looks like:
- embed-{post_type}-{post_format}.php
- embed-{post_type}.php
- embed.php
An example of this is:
- embed-post-audio.php
- embed-post.php
- embed.php
The template hierarchy and template path are filterable via the ‘$type_template_hierarchy’ and ‘$type_template’ dynamic hooks, where $type is ’embed’.
See also
Source
function get_embed_template() {
$object = get_queried_object();
$templates = array();
if ( ! empty( $object->post_type ) ) {
$post_format = get_post_format( $object );
if ( $post_format ) {
$templates[] = "embed-{$object->post_type}-{$post_format}.php";
}
$templates[] = "embed-{$object->post_type}.php";
}
$templates[] = 'embed.php';
return get_query_template( 'embed', $templates );
}
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |