Related items: filter out current page

Hi,

I use the following code to display related work at the bottom of a portfolio child page:

<?php snippet('portfolio', [
    'portfolio' => page('portfolio')
    ->children()
    ->listed()
    ->shuffle()
    ->limit(3)
]) ?>

And my snippet:

<?php foreach ($portfolio as $item): ?>
  <div class="item">
    <a href="<?= $item->url() ?>">
      <img src="<?= $item->cover()->crop(800, 800, 80)->url() ?>" width="<?= $item->cover()->crop(800, 800)->width() ?>" height="<?= $item->cover()->crop(800, 800)->height() ?>" alt="">
    </a>
  </div>
<?php endforeach ?>

How can i filter out the current page to be displayed in the related section?

Instead of fetching the parent page, use the current page and the siblings() method:

<?php snippet('portfolio', [
    'portfolio' => $page
    ->siblings(false) // setting the `$self` param to `false`leaves out the current page
    ->listed()
    ->shuffle()
    ->limit(3)
]) ?>
1 Like

Just to show how flexible Kirby is, you can also do it this way with filterby(), in a collection to get your main page set, and the chain the limit() and shuffle() on the end as required when you use it on the sinppet…

From my own portfolio…

<?php
return function ($site) {
    $portfolio = $site->find('work')->children()->listed()->flip()->filterby('title', '!=', page()->title());
    return $portfolio;
};
1 Like

Fantastic! Thanks for the quick solution. Kirby and the community rocks!