Item Count in Nav Menu

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!

You can use ->count()

1 Like

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>
    <?php endforeach ?>
1 Like

Hey that worked perfectly, thank you!