Item Count in Nav Menu


#1

Simple question, but I can’t find any documentation on how to implement the following.

How might I implement a count of the number of sub pages in a item menu. For example Articles menu item to render the number of articles eg. Articles (12) etc.

Thanking you in advance!


#2

You can use ->count()


#3

Here is an example with the navigation of the StarterKit. We check if the item has children and print the number of children if true:

<nav class="navigation column" role="navigation">
  <ul class="menu">
    <?php foreach($pages->visible() as $item): ?>
    <li class="menu-item<?= r($item->isOpen(), ' is-active') ?>">
      <a href="<?= $item->url() ?>"><?= $item->title()->html() ?><?= $item->hasChildren()? ' ('.$item->children()->count() .')':null ?></a>
    </li>
    <?php endforeach ?>
  </ul>
</nav>

#4

Hey that worked perfectly, thank you!