get_attachment_template

函式
get_attachment_template ( No parameters )
返回值
  • (string) Full path to attachment template file.
相關
  • get_query_template()
定義位置
相關方法
get_home_templateget_date_templateget_archive_templatewp_get_attachment_imageget_tag_template
引入
2.0.0
棄用
-

get_attachment_template: 這個函式檢索當前主題的附件模板的路徑。

檢索當前或父模板中的附件模板的路徑。

這個模板的層次結構看起來像:

1. {mime_type}-{sub_type}.php
2. {sub_type}.php
3. {mime_type}.php
4. attachment.php

這方面的一個例子是:

1. image-jpeg.php
2. jpeg.php
3. image.php
4. attachment.php

模板層次和模板路徑可通過{@see ‘$type_template_hierarchy’}和{@see ‘$type_template’}動態鉤子過濾,其中`$type’為’附件’。

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 );
}

常見問題

FAQs
檢視更多 >