Use an Image From a URL With Timber
Categories
Most of the time when creating a WordPress website based on Timber I am working with local images that were added through the WordPress media library or uploaded using an ACF image field. You use these with the help of the get_image()
function of Timber.
But sometimes it can come in handy to work with images from an URL source – be it from another site or even your own server.
Here is how to do that with the get_external_image()
function:
$context = Timber::context();
// Lets say you have an external image that you want to use in your theme
$context['cover_image'] = Timber::get_external_image($url);
Timber::render('single.twig', $context);
Then in your twig file you can use the typical filters and properties:
<img src="{{ cover_image.src }}" class="cover-image" />
Helpful documentation for further information: Timber\externalImage and work with external images.