Get files from page with selected size value


Im creating my first Portfolio Page with Kirby. In my project template I want to fetch the uploaded images filtered by a size option that can be defined for each image.

This is the default.yml file for each image:

title: Default File

    label: size
    type: select
      - small
      - medium
      - big
    width: 1/2
    label: Caption
    type: textarea

This is how far I’ve come with my project.php template (sadly doesn’t work like i want it to):

    <?php snippet('header') ?>

    <section class="image-container">
      <?php foreach($page->files()->filterBy('size', 'medium') as $image): ?>
      <figure class="quarter-image">
          <?= $image->crop(2000, 1200) ?>
      <?php endforeach ?>

    <?php snippet("CTA") ?>
    <?php snippet('footer') ?>

As Im really new to working with kirby and php in general im guessing there’s an easy solution for this that i just cant get on my own.

I think size() is a native kirby method. Try renaming your size field to something else.

If you can’t do that, try:

$images = $page->files()->filter(function($file) {
  return $file->content()->size() == 'medium';
foreach($images as $image): ?>

I changed the Name of the Field and now it works perfectly, thank you very much!