Timber Logo

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​Attachment

Objects of this class represent WordPress attachments. This is the basis that Timber\Image objects build upon.

Overview #

This class extends Timber\Post

Properties #

NameTypeDescription
$filestring
$file_locstringThe absolute path to the attachmend file in the filesystem (Example: /var/www/htdocs/wp-content/uploads/2015/08/my-pic.jpg)
$file_extensionstringA file extension.

Methods #

NameReturn TypeSummary/Returns
__toString()stringGets the src for an attachment.

Returns: The src of the attachment.
caption()string or nullGets the caption of an attachment.
extension()stringGets the extension of the attached file.

Returns: An uppercase extension string.
file()stringGets the relative path to the uploads folder of an attachment.
file_loc()stringGets the absolute path to an attachment.
link()stringGets the link to an attachment.

Returns: The URL of the attachment.
parent()null or \Timber\PostGets the parent object.

Returns: Parent object as a Timber\Post. Returns false if no parent object is defined.
path()stringGets the relative path to an attachment.

Returns: The relative path to an attachment.
size()int or nullGets the raw filesize in bytes.

Returns: The raw filesize or null if it could not be read.
src()stringGets the source URL for an attachment.

Class Methods #

__toString() #

Gets the src for an attachment.

Returns: string The src of the attachment.


Gets the link to an attachment.

This returns a link to an attachment’s page, but not the link to the image src itself.

Returns: string The URL of the attachment.

Twig

<a href="{{ image.link }}"><img src="{{ image.src }} "></a>

HTML

<a href="http://example.org/my-cool-picture">
<img src="http://example.org/wp-content/uploads/2015/whatever.jpg"/>
</a>

path() #

Gets the relative path to an attachment.

Returns: string The relative path to an attachment.

Twig

<img src="{{ image.path }}" />

HTML

<img src="/wp-content/uploads/2015/08/pic.jpg" />

file() #

Gets the relative path to the uploads folder of an attachment.

Returns: string


file_loc() #

Gets the absolute path to an attachment.

Returns: string


src() #

Gets the source URL for an attachment.

Returns: string

Twig

<a href="{{ get_attachment(post.meta('job_pdf')).src }}" download>

HTML

<a href="http://example.org/wp-content/uploads/2015/08/job-ad-5noe2304i.pdf" download>

caption() #

Gets the caption of an attachment.

since 2.0

Returns: string|null

Twig

<figure>
<img src="{{ post.thumbnail.src }}">

{% if post.thumbnail is not empty %}
<figcaption>{{ post.thumbnail.caption }}</figcaption
{% endif %}
</figure>

size() #

Gets the raw filesize in bytes.

Use the size_format filter to format the raw size into a human readable size («1 MB» intead of «1048576»)

see https://developer.wordpress.org/reference/functions/size_format/ Use filesize information in a link that downloads a file:

<a class="download" href="{{ attachment.src }}" download="{{ attachment.title }}">
<span class="download-title">{{ attachment.title }}</span>
<span class="download-info">(Download, {{ attachment.size|size_format }})</span>
</a>

since 2.0.0

Returns: int|null The raw filesize or null if it could not be read.


extension() #

Gets the extension of the attached file.

since 2.0.0

Returns: string An uppercase extension string.

Use extension information in a link that downloads a file:

Twig

<a class="download" href="{{ attachment.src }}" download="{{ attachment.title }}">
<span class="download-title">{{ attachment.title }}</span>
<span class="download-info">
(Download {{ attachment.extension|upper }}, {{ attachment.size }})
</span>
</a>

parent() #

Gets the parent object.

The parent object of an attachment is a post it is assigned to.

Returns: null|\Timber\Post Parent object as a Timber\Post. Returns false if no parent object is defined.

Twig

This image is assigned to {{ image.parent.title }}