Detect if child page is active?

This must be trivial but I can’t figure out how to detect wether a child page of my current page is active.

There is a function isDescendantOfActive() but unfortunately not isAncestorOfActive().
I want to add class=is-current to the element with class=has-dropdown not only when that page is open but also when one of its descendants are:

<div class="navbar">
<?php foreach($items as $item):
$children = $item->children()->visible();
if($children->count() > 0): ?>
<div class="navbar-item has-dropdown <?php e($item->isOpen(), ' is-current') ?>">
  <a class="navbar-link">
    <?= $item->title()->html() ?>
  </a>
  <div class="navbar-dropdown">
    <?php foreach($children as $child): ?>
        <a class="navbar-item <?php e($child->isOpen(), ' is-current') ?>" href="<?php echo $child->url() ?>">
        <?= $child->title()->html() ?>
        </a>
    <?php endforeach ?>
  </div>
</div>
<?php endif ?>
<?php endforeach ?>
</div>

How would i do that?

Your code should actually work as is because isOpen() actually checks if it is either the active page or a direct or indirect parent of the currently active page.

2 Likes

And it does! I am officially a moron.

Thanks a bunch!