Skip to main content

Attachment

Attachment objects represent non-image file attachments in WordPress (PDFs, documents, videos, audio files, etc.). They appear when iterating over attachments in a Loop block or chaining into attachment fields. The attachment root reads the current post as a file: on an attachment page, or for each item of a loop over attachments.

For image attachments, see Image.

Attachment inherits everything from Post — id, title, slug, link, dates, author, parent, meta, edit_link, raw post_* fields, etc. The fields below are attachment-only.

In the WP media UI, the post fields are renamed:

  • content is the attachment Description
  • post_excerpt is the attachment Caption as stored; caption filters it. excerpt is the post excerpt builder: it falls back to the description when there's no caption.

All Fields​

FieldArgumentsReturnsDescription
src—stringFile URL
caption—stringCaption (filtered through wp_get_attachment_caption)
file—stringRelative file path (to uploads folder)
file_loc—stringServer file path (private)
extension—stringFile extension
size—int | nullFile size in bytes
pathinfo—arraypathinfo() array (basename, extension, filename; dirname is private)

Common Patterns​

Display file type badge​

{{ item.extension|upper }}

src is the file URL; link is the attachment page, as in Timber:

<a href="{{ item.src }}">{{ item.title }} ({{ item.extension|upper }})</a>

Show file size​

{{ item.size|number_format }} bytes

File type icon class​

file-{{ item.extension }}