How do I set the alt text on resized images in a loop?
<?php foreach($page->images() as $image):
echo $image->resize(300);
endforeach ?>
This will output the images, but the ALT is empty.
Thanks.
How do I set the alt text on resized images in a loop?
<?php foreach($page->images() as $image):
echo $image->resize(300);
endforeach ?>
This will output the images, but the ALT is empty.
Thanks.
You can use the thumb()
and html()
methods:
<?php foreach($page->images() as $image):
echo $image->thumb(['width' => 300])->html(['alt' => $image->alt()->html()]);
endforeach ?>
Particularly I prefer to add the HTML myself:
<?php foreach($page->images() as $image): ?>
<figure>
<img src="<?= $image->thumb(['width' => 300])->url() ?>" alt="<?= $image->alt()->html() ?>" />
</figure>
<?php endforeach ?>
I see. Using thumb() instead of resize works for me! Thanks.