Timber: Get single image from media library with all attributes

In my Timber-based theme, I’m getting a single image from the media library using the following (based on Timber/Image):

<img src=”{{ Image(678).src }}” alt=”{{ Image(678).alt }}” srcset=”{{ Image(678).srcset }}” sizes=”{{ Image(678).img_sizes }}” />

However it’s silly to repeatedly state the image id for each attribute.

Is there a single command that can spit out a fully populated image instance or does this call for a custom PHP function, and make it available to Timber?

For reference, the above code outputs this:

<img src=”https://mywebsite.com/wp-content/uploads/2019/11/myimage.png” alt=”This is the image!” srcset=”https://mywebsite.com/wp-content/uploads/2019/11/myimage.png 1052w, https://mywebsite.com/wp-content/uploads/2019/11/myimage-150×150.png 150w, https://mywebsite.com/wp-content/uploads/2019/11/myimage-150×150@2x.png 300w, https://mywebsite.com/wp-content/uploads/2019/11/myimage-768×768.png 768w, https://mywebsite.com/wp-content/uploads/2019/11/myimage-1024×1024.png 1024w, https://mywebsite.com/wp-content/uploads/2019/11/myimage-300×300@2x.png 600w” sizes=”(max-width: 1052px) 100vw, 1052px”>

Read more here:: Timber: Get single image from media library with all attributes

Leave a Reply

Your email address will not be published. Required fields are marked *