Accepting multiple image formats

Hello again!

We’ve setup a content folder where all blog posts are organised by folder. Currently, our code accepts ‘header.png’ as the header of a blog post. So far so good.

We would like to add jpg/jpeg/gif as an option as well. It just looks for either one of those extensions. And we will manage that there is only 1 image file at a time.

Anyone with experience in this area? ty!!!

      <div class="blog-image" style="background-color: <?php echo $page->Colour() ?>;">
        <?php if($image = $page->image('header.png')): ?>
          <img src="<?php echo $image->url() ?>" />
        <?php endif ?>
      </div>
    </div>

Try

$image = $page->images()->filterBy('name', 'header')->first();

If there is really no other image, you can also use:

$image = $page->file();

which will return the first file in the folder, https://getkirby.com/docs/cheatsheet/page/file

1 Like

Thank you - resolved.