How to add "active" class only for first DIV in a carousel slider?

As a Kirby newbie, many questions arise. Each project is too individual to find exact solutions.
My question: I use a carousel slider. Reading the images from the folder works perfectly. For the slider to work, only the first DIV box from the carousel must contain the class “active”:

<div class="carousel-item active"><img src="<?= $file->url() ?>" alt="" /></div>

How do I insert the variable that only the first DIV contains this class?
Thanks a lot for your support!

Check if the current file is the same as the first in the collection:

<?php
$files = $page->files();
foreach ($files as $file): ?>
  <div class="carousel-item <?= $file->is($files->first()) ? 'active' : '' ?>"><img src="<?= $file->url() ?>" alt="" /></div>
<?php endforeach ?>

Adapt to fit your variable definitions.

1 Like

Thanks a lot! Your solution worked immediately. Great :clap: