主题开发文档

附件模板文件

💡 云策文档标注

概述

附件是 WordPress 中一种特殊的文章类型,用于存储通过媒体上传系统上传的文件信息,如描述和名称,可通过多种附件模板文件显示。利用附件模板可以获取上传文件的额外元数据,有助于 SEO 优化。

关键要点

  • 附件模板基于 MIME 类型和子类型提供自定义显示方式,例如 image.php 用于所有图像,jpg.php 用于 JPEG 子类型。
  • 模板层级结构定义了附件显示的优先级顺序,从 MIME_type.php 到 index.php,确保兼容性。
  • attachment.php 文件用于创建附件页面,类似于 single.php,可包含 wp_get_attachment_image 等函数来显示附件内容。
  • get_attachment_template() 函数用于检索当前或父模板中的附件模板路径。

代码示例

<div class="entry-attachment">
	<?php
	$image_size = apply_filters( 'wporg_attachment_size', 'large' );
	echo wp_get_attachment_image( get_the_ID(), $image_size );
	?>

	<?php if ( has_excerpt() ) : ?>
		<div class="entry-caption">
			<?php the_excerpt(); ?>
		</div><!-- .entry-caption -->
	<?php endif; ?>
</div><!-- .entry-attachment -->

📄 原文内容

Attachments are a special post type that holds information about a file uploaded through the WordPress media upload system, such as its description and name, which can display through several post type – attachment template files.

For images, as an example, the attachment post type links to metadata information, about the size of the images, the thumbnails generated, the location of the image files, the HTML alt text, and even information obtained from EXIF data embedded in the images.

Utilizing attachment templates to gain additional metadata information for uploads, help with SEO efforts.

As shown in the template hierarchy, you are able to display your attachments through several template files in order of fallback:

  1. MIME_type.php and a subtype.php
    It can be any MIME type (For example: image.php, video.php, application.php). For text/plain, the following path is used (in order):
    1. text_plain.php
    2. plain.php
    3. text.php
  2. attachment.php
  3. single-attachment.php
  4. single.php
  5. singular.php
  6. index.php

MIME_type.php

Attachments are served by template files based on their mime-type. As an example, if your attachment is an image, your can customize how they display through the creation of an image.php template file. All images with the post_mime_type of image/* will render though your image.php template file.

Attachments also support the use of a mime subtype.php file. To continue with the image example, you can further customize your theme to support not only an image.php file but a jpg.php subtype file.

Attachment.php

An attachment page (attachment.php) is a single post page with the post type of attachment, generated through the creation of an attachment.php. Just like a single post page, which is dedicated to your article, the attachment page provides a dedicated page in attachments in your theme.

Creation of attachment page is as simple as creating an attachment.php file. The attachment.php file then contains code similar to the single.php post page.

<div class="entry-attachment">
	<?php
	$image_size = apply_filters( 'wporg_attachment_size', 'large' );
	echo wp_get_attachment_image( get_the_ID(), $image_size );
	?>

	<?php if ( has_excerpt() ) : ?>
		<div class="entry-caption">
			<?php the_excerpt(); ?>
		</div><!-- .entry-caption -->
	<?php endif; ?>
</div><!-- .entry-attachment -->

Function Reference