Order projects by the page you are on

I would like to sort my projects depending on wich page you are on.
At first is shown the project you clicked on (basically $page)
followed by all the remaining projects, minus the one at the top.

<?php foreach($site->find('projects')->children()->sortBy(function ($page) {
  return $page->title();
}, 'asc') as $project): ?>

<main class="project" id="<?= $project->indexOf() ?>">

    <?php foreach($project->images()->sortBy('sort') as $image): ?>
        <img src="<?= $image->url() ?>" alt="<?= $image->description() ?>">
    <?php endforeach ?>
</main>

What is your use case here? When you are on the single page, you probably want to show more info about this specific page then about the sibling pages? Otherwise, this page would look the same as the overview page.

I’d probably do this in two parts:

  1. Show information about the current page (using $page)
  2. Fetch the siblings without the current page ($page->siblings(false))

Well, while posting the question, I found another solution,
using $page->not()

<main class="project">
  <?php foreach($page->images()->sortBy('sort') as $image): ?>
        <img src="<?= $image->url() ?>" alt="<?= $image->description() ?>">
    <?php endforeach ?>
</main>

<?php foreach($site->find('projects')->children()->not($page) as $project): ?>
<main class="project" id="<?= $project->indexOf() ?>">

    <?php foreach($project->images()->sortBy('sort') as $image): ?>
        <img src="<?= $image->url() ?>" alt="<?= $image->description() ?>">
    <?php endforeach ?>
</main>
<?php endforeach ?>

Not 100% sure, but I’d expect $page->siblings(false) to be more performant, instead of trying to find the right page and its children again.

thanks, it’s working great too.