Slider: Only show structure field slide if it's not empty

Hello,

I have a slider with images and the last slide always displays some text pulled from a structure field. The problem is sometimes I don’t want to add any text and at the moment I get an empty slide when I don’t add any content. I want to only show the last slide if the structure field is not empty. I’ve already tried a few things but I can’t make it work, either I completely delete the text slides from all the projects or it just keeps showing blank slides for the projects without text.

Here’s my code:

<?php foreach ($page->proj()->toStructure() as $item): ?>

<?php if($item->description()->html()) : ?>

    <div class="swiper-slide">
    <div class="slide-container">

<div class="project-description">
        <p class="proj-text">
        <?= $item->description()->html() ?>
        </p>
      </div>
        <div class="proj-infos">
        <p class="info-name"><?= $item->placetitle() ?></p>
          <p class="info-description"><?= $item->place() ?></p>
          <p class="info-name"><?= $item->materialtitle() ?></p>
          <p class="info-description"><?= $item->material() ?></p>
          <p class="info-name"><?= $item->typetitle() ?></p>
          <p class="info-description"><?= $item->type() ?></p>
          <p class="info-name"><?= $item->functiontitle() ?></p>
          <p class="info-description"><?= $item->function() ?></p>
          <p class="info-name"><?= $item->sizetitle() ?></p>
          <p class="info-description"><?= $item->size() ?></p>
          <p class="info-name"><?= $item->creditstitle() ?></p>
          <p class="info-description"><?= $item->credits()->kti() ?></p>
        </div>

      </div>     
</div>
<?php endif ?>
<?php endforeach ?>  

I appreciate any help since I don’t understand why this code doesn’t work,

thank you so much!

Is it about the description() field?
You don’t need html() in the if query, this helper is only needed for the output:

You should also perform the if query first before using the foreach loop:

<?php if($item->description()->isNotEmpty()): ?>
<?php foreach ($page->proj()->toStructure() as $item): ?>

[…]

<?php endforeach ?>
<?php endif ?>

I tried it already but it doesn’t work and I dont know why! Basically I want to only show a slide if at least one field is not empty it doesn’t have to be the description but I am not able to achieve this

Then you can expand the if query.
For example:

<?php if($item->placetitle()->isNotEmpty() || $item->materialtitle()->isNotEmpty()): ?>

If you tell me which fields are optional and which fields must be filled in, I can change your code accordingly.


You can also assign an if query to each field.
Unfortunately, the code now looks very bloated, but you have a very extensive structure field.

<?php if($item->description()->isNotEmpty()): ?>
<?php foreach ($page->proj()->toStructure() as $item): ?>

<div class="swiper-slide">
<div class="slide-container">

<div class="project-description">
<p class="proj-text">
<?= $item->description()->html() ?>
</p>
</div>

<div class="proj-infos">
<?php if($item->placetitle()): ?><p class="info-name"><?= $item->c() ?></p><?php endif ?>
<?php if($item->place()): ?><p class="info-description"><?= $item->place() ?></p><?php endif ?>
<?php if($item->materialtitle()): ?><p class="info-name"><?= $item->materialtitle() ?></p><?php endif ?>
<?php if($item->material()): ?><p class="info-description"><?= $item->material() ?></p><?php endif ?>
<?php if($item->typetitle()): ?><p class="info-name"><?= $item->typetitle() ?></p><?php endif ?>
<?php if($item->type()): ?><p class="info-description"><?= $item->type() ?></p><?php endif ?>
<?php if($item->functiontitle()): ?><p class="info-name"><?= $item->functiontitle() ?></p><?php endif ?>
<?php if($item->functionRENAME()): ?><p class="info-description"><?= $item->functionRENAME() ?></p><?php endif ?>
<?php if($item->sizetitle()): ?><p class="info-name"><?= $item->sizetitle() ?></p><?php endif ?>
<?php if($item->size()): ?><p class="info-description"><?= $item->size() ?></p><?php endif ?>
<?php if($item->creditstitle()): ?><p class="info-name"><?= $item->creditstitle() ?></p><?php endif ?>
<?php if($item->credits()->kti()): ?><p class="info-description"><?= $item->credits()->kti() ?></p><?php endif ?>
</div>

</div>     
</div>

<?php endforeach ?>
<?php endif ?>

Tip: Avoid field names such as function() as these are used internally by Kirby.

Thank you I will try this, all the fields are optional that’s why if all are empty the slide should not be shown! Is there a way to query all of them together or do I need to enter it manually? Thank you ao much for your help

Do you have a screenshot of how the slider including text should look like?
<p class="info-name"> and <p class="info-description"> are repeated, but as you use numerous field names, it is difficult to create a compact representation without knowing what the slider looks like.

I just realised that if I delete the field from the panel and only add it when I want to add text in the panel, the slider no longer shows an empty slide this way I don’t need to query the fields if all of them are empty. thank you so much for your help and tip regarding the name, I will change the function name!