Reference

Guide

$pages->groupBy()

Groups the items by a given field

$pages->groupBy(string $field, bool $i = true): Collection

Parameters

Name Type Default Description
$field string
$i bool true

(ignore upper/lowercase for group names)

Return type

Collection

Example:

<?php
$years = page('projects')->children()->groupBy('year');
foreach($years as $year => $items): ?>
    <h2><?= $year ?></h2>
    <ul>
      <?php foreach($items as $item) : ?>
      <li><?= $item->title() ?></li>
      <?php endforeach; ?>
    </ul>
<?php endforeach ?>

Inherited from

Kirby\Cms\Collection

Source code

kirby/src/Cms/Collection.php#L128