If there is less then 1 in count do not show element?

I am trying to check if there are 0 images with the field name: carouselImages, then do not show the hide all including the parent.

    <?php if($page->carouselImages()->count() > 1) echo>
    <div class="carousel-inner">
        <?php $n=0; foreach($page->carouselImages()->toFiles() as $image): $n++; ?>
            <div class="item<?php if($n==1) echo ' active' ?>">
                <img src="<?php echo $image->url() ?>"/>
            </div>
        <?php endforeach ?>
    </div>

This worked for me

<?php if ($page->carouselImages()->isNotEmpty()): ?>
     <div class="carousel-inner">
    </div>
<?php endif ?>

@pixelijn It turns out this doesn’t work.
How do I only show this section is the count is equal or more than 1?

<?php if ($page->carouselImages()->count()): ?>
     <div class="carousel-inner">
    </div>
<?php endif ?>

As I already wrote in my post above, you have to convert the field value to a files object first

if ( $page->carouselImages()->toFiles()->isNotEmpty() )

Okay thanks, I am now getting this error: syntax error, unexpected token “endif”, expecting end of file

I was missing a colon, here is the full line:

<?php if ($page->carouselImages()->toFiles()->isNotEmpty()):?>

@pixelijn - I am trying to do the same thing with a related articles element. Do not show if the page has no siblings. This doesn’t work, is there a different method for this?

<?php if ($page->siblings()->isNotEmpty()):?>
     <div class="row">
     </div>
<?php endif ?>

afaik: isNotEmpty() is a field method, but $page->siblings() returns a collection, so:

if $page->siblings()->count() > 0

Okay thanks, this worked better for me:

<?php if ($page->siblings()->count() > 1):?>

@bruno

1 Like

ah. sorry. no idea why it didn’t work in the first place then

The reason why $page->siblings()->isNotEmpty() doesn’t work as expected is the siblings method which by default includes the current page:

Exclude it, and it will work:

$page->siblings(false)->isNotEmpty()
1 Like