$collection->group($callback)

Groups the collection by a given callback

  • since: Kirby 2.3.0
  • $callback (function)
    the callback function
  • return ($collection)

Example:

<?php 

// group items by year
$groupedItems = $page->children()->visible()->group(function($p){
  return $p->date('Y');
});

foreach ($groupedItems as $year => $yearList): ?>
  <h2><?= $year ?></h2>
  <ul>
    <?php foreach($yearList as $article): ?>
      <li><a href="<?= $article->url() ?>"><?= $article->title() ?></a></li>
    <?php endforeach ?>
  </ul>
<?php endforeach ?>