get_attachment_template()

获取附件页面模版文件attachment.php

描述

检索当前模板或父模板的附件模板路径。

附件路径首先查找mime类型的第一部分是否存在,接着超找第二部分。最后查找由下划线分成的两个类别。如果没有查找到任一部分,可查找’attachment.php’文件并返回。

Mime类别’text/plain’的示例包括’text.php’, ‘plain.php’, 以及 ‘text_plain.php’。

用法

<?php get_attachment_template() ?>

参数

None.

返回值

(string) 

Returns path of attachment template in current or parent template.

注意

  • 使用到: get_query_template()
  • 使用到 global: (object) $posts a property of the WP_Query object.

历史

添加于 版本: 2.0.0

源文件

get_attachment_template() 函数的代码位于 wp-includes/template.php.

/**
* Retrieve path of attachment template in current or parent template.
*
* The attachment path first checks if the first part of the mime type exists.
* The second check is for the second part of the mime type. The last check is
* for both types separated by an underscore. If neither are found then the file
* ‘attachment.php’ is checked and returned.
*
* Some examples for the ‘text/plain’ mime type are ‘text.php’, ‘plain.php’, and
* finally ‘text-plain.php’.
*
* The template path is filterable via the dynamic {@see ‘$type_template’} hook,
* e.g. ‘attachment_template’.
*
* @since 2.0.0
*
* @see get_query_template()
*
* @global array $posts
*
* @return string Full path to attachment template file.
*/

function get_attachment_template() {

$attachment = get_queried_object();

$templates = array();

if ( $attachment ) {

if ( false !== strpos( $attachment->post_mime_type, ‘/’ ) ) {

list( $type, $subtype ) = explode( ‘/’, $attachment->post_mime_type );

} else {

list( $type, $subtype ) = array( $attachment->post_mime_type, ” );

}

if ( ! empty( $subtype ) ) {

$templates[] = “{$type}-{$subtype}.php”;

$templates[] = “{$subtype}.php”;

}

$templates[] = “{$type}.php”;

}

$templates[] = ‘attachment.php’;

return get_query_template( ‘attachment’, $templates );

}

原文:http://codex.wordpress.org/Function_Reference/get_attachment_template

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索