Submenu with $root, children and an "isOpen()"-Problem

Hello. I am trying to get a submenu that includes the children AND (on top) the $root.

<nav class="sub-nav" role="navigation">
    <?php
    $items = false;
    //get the open item on the first level
    if($root = $pages->findOpen()) {
        //get visible children for the root item
        $items = $root->children()->visible();
    }
     // only show the menu if items are availlable
     if($items and $items->count()): ?>
         <ul>
             <li><a href="<?php echo $root->url() ?>"><?php echo $root->title() ?></a></li>
             <?php foreach($items as $item): ?>
             <li><a<?php e($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo $item->title()->html() ?></a></li>
             <?php endforeach ?>
         </ul>
     </nav>
     <?php endif ?>

Now i’d like to have the $root marked as active (when it’s active) like it’s working on the children. But if i use $root->isOpen(), 'class="active" the root is always marked as active even when i’m on a child page.

Any idea how i can get this thing done? Thanks in advance.

In Kirby there are two similar methods for this, $page->isOpen() and $page->isActive():

  • $page->isActive() is true when $page is the current page.
  • $page->isOpen() is also true when $page is a parent of the current page.

If you only want to mark $root if it itself is current page, try isActive() instead of isOpen().

3 Likes