Skip to content

Kirby 4.4.1

$page->indexOf()

Returns the position / index in the collection

$page->indexOf(\Kirby\Cms\Collection|null $collection = null): int|false

Parameters

Name Type Default
$collection Kirby\Cms\Collection|null null

Return type

int|false

Parent class

Kirby\Cms\Page

Example

<?php $children = $page->children()->listed(); ?>
<ul>
  <?php foreach ($children as $child): ?>
  <li class="<?php e(($child->indexOf($children) + 1) % 2, 'some-class', 'another-class') ?>">
        <?= $child->title() ?>
    </li>
<?php endforeach ?>
</ul>

This is just a constructed example. It's worth noting that for simple styling use, you could use CSS only.