How to access Alt Text

Hi,

I have a quick question. How do I return an Alt Text from an image in my template?

Thanks!

You can get to it once you have a file object ready. Simplest example, assuming you have image store in a field, and you filled out the alt field in the files meta file:

<?php if($img = $page->yourimagefield()->toFile()): ?>
  <img src="<?= $img->url() ?>" alt="<?= $img->alt() ?>">
<?php endif ?>

Or directly on a file name (untested but should work if the files meta file exists and the alt field has a value):

<?= image('forrest.jpg')->alt() ?>

Basically once you have an valid file either with toFile() or image() you can then access anything in its meta file by chaining ->metafieldname() on to it. Obviously replace that with the actual name of a field in the meta :slight_smile:

3 Likes

Hi @jimbobrjames,

Awesome you are a legend!
Thank you :pray: