Skip to content

Kirby 3.8.4

$collection->group()

Groups the items by a given field or callback. Returns a collection with an item for each group and a collection for each group.

$collection->group(string|\Closure $field, bool $i = true): Kirby\Cms\Collection

Parameters

Name Type Default Description
$field * string|Closure
$i bool true Ignore upper/lowercase for group names

Return type

Kirby\Cms\Collection

Exceptions

Type Description
Kirby\Exception\Exception

Parent class

Kirby\Cms\Collection

Example

<!-- group the collection by the field `category` -->
<?php foreach ($collection->group('category') as $category => $group): ?>
  <h2><?= $category ?></h2>
  <ul>
    <?php foreach ($group as $item): ?>
    <li><?= $item->title() ?></li>
    <?php endforeach ?>
  </ul>
<?php endforeach ?>