Can't fix If-else issue

Hi there,

I have a question that I can’t solve. I would like to show related projects inside a news article. In the panel (backend) you can choose which projects are related to a news article.

It all works fine. Until I don’t link a project inside an news article. Then I get an empty gray area (the place where a project is shown when it is linked). But if no projects are linked, this area should of course not be shown.

It has obbvious something to do whit the if else loop and the counts but I have tried almost every combination (I know, not every). I am also wondering if this is also possible with the topages function. Something like:

<?php $slugs = $page->projectlink()->toPages(); if ($slugs->count() > 0): ?>

This is my code:

<?php $slugs = explode(', ',$page->projectlink()); ?>
<?php if(count($slugs) == 0) : ?>

<!-- do nothing -->

<?php elseif (count($slugs) >= 1) : ?>

<div class="wrapper-small projectlink">

    <div class="column-section clearfix spaced-none">
        <h5 style="padding-bottom: 1rem; font-weight: 400;">
            <?php echo count($slugs) > 1 ? 'Related projects' : 'Related project'; ?>
        </h5>

        <?php foreach($slugs as $slug){ ?>

        <?php if($relatedproject = page($slug)) : ?>

        <div class="column two-fifth">
            <?php if ($image = $relatedproject->images()->findBy('template', 'cover')) : ?>
            <a href="<?= $relatedproject->url() ?>">
                <div class="team-member">
                        <img class="team-pic" src="<?= $image->crop(500, 320)->url() ?>" alt="<?= $image->alt() ?>">
                    </div>
                </div>
            </a>
            <?php endif ?>
        </div>

        <?php endif ?>
        <?php } ?>
    </div>
</div>
<?php endif ?>

Question: Looks as if you are storing your links in a multiselect rather than a pages field?

Yes, you can use to pages with both the pages field or a list of comma separated ids, only difference is you have to pass the separator for the comma separated list:

$slugs = $page->projectlink()->toPages(',');

Your code looks a bit messed up:

<?php $relatedProjects = $page->projectlink()->toPages(','); ?>

<?php if ($relatedProjects->count() >= 1) : ?>

    <div class="wrapper-small projectlink">

        <div class="column-section clearfix spaced-none">
            <h5 style="padding-bottom: 1rem; font-weight: 400;">
                <?php echo $relatedProjects->count() > 1 ? 'Related projects' : 'Related project'; ?>
            </h5>

            <?php foreach($relatedProjects as $relatedProject) : ?>
                <div class="column two-fifth">
                    <?php if ($image = $relatedProject->images()->findBy('template', 'cover')) : ?>
                        <a href="<?= $relatedProject->url() ?>">
                            <div class="team-member">
                                <img class="team-pic" src="<?= $image->crop(500, 320)->url() ?>" alt="<?= $image->alt() ?>">
                            </div>
                        </a>
                    <?php endif ?>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
<?php endif ?>

If you don’t want to do anything if count is zero, you don’t have to cover that case at all, so I left it out here.

But what do you mean with the gray space where the project should be?

Thanks you!
That’s correct I am using a multiselect limited to 3 projects.
My blueprint:

  projectlink:
    label: Linked projects
    type: multiselect
    options: query
    max: 3
    query: site.children.template("projects").children

Should I use pages field?

That’s completely up to you!

The grey field/space is a (css) wrapper.

Thank you @texnixe for you help. I’ve been messing around with it all day and could fix it with your help! :pray: