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:
contentis the attachment Descriptionpost_excerptis the attachment Caption as stored;captionfilters it.excerptis the post excerpt builder: it falls back to the description when there's no caption.
All Fields
| Field | Arguments | Returns | Description |
|---|---|---|---|
src | — | string | File URL |
caption | — | string | Caption (filtered through wp_get_attachment_caption) |
file | — | string | Relative file path (to uploads folder) |
file_loc | — | string | Server file path (private) |
extension | — | string | File extension |
size | — | int | null | File size in bytes |
pathinfo | — | array | pathinfo() array (basename, extension, filename; dirname is private) |
Common Patterns
Display file type badge
{{ item.extension|upper }}
Link to download
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 }}