sortBy number and groups my projects


For a project I need to organize projects by years, the intention it’s to layout in the frontend my project like this :

  • 2021
    project A
    project B

  • 2020
    project C
    project D

For the moment, I manage to sort my project by year and display the years. But I don"t really know how I can do the layout I want.

This is what I have for the moment :

  • 2021

  • 2020

  • project A

  • project B

  • project C

  • project D

Here’s my code :

    <?php  foreach ($pages->find('projets')->children()->pluck('year', ',', true) as $year): ?>
        <datetime><?= $year ?></datetime>
    <?php endforeach ?>

      $articles = page('projets')->children()->listed()->sortBy(function ($page) {
        return $page->number()->toNumber();
      foreach($articles as $article): ?>
        <!-- Looping through blocks, to get some blocks fields -->
        <?php foreach ($article->cover()->toBlocks() as $block): ?>

          <a class="column-<?= $block->layout() ?>" href="<?= $article->url() ?>">
            <h2><?= $article->number()->toNumber() ?></h2>
            <?= $block ?>
              <?= $article->title() ?>
        <?php endforeach ?>
    <?php endforeach ?>

I was thinking by sorting byGroup. But I’m not really sure.
If you have a suggestion, I’ll be glad to look at it.


Check out this cookbook recipe:

1 Like