Check for active page not working

I have the problem that the first entry in the foreach function is always shown as active. Have I overlooked something?

Kirby Version: 3.8.1.1

Home is unlisted.

My Codes that i tried:

<?php
        // main menu items
        $items = $pages->listed()->filterBy('bottomMenu', false);
        // only show the menu if items are available
        if($items->isNotEmpty()):
?>
 // loops listed below
<?php endif ?>
<?php foreach ($items as $item): ?>
            <li<?php e($item->isActive() && !$item->isHomeOrErrorPage() && $item->isListed(), ' class="current"') ?>>
              <a href='<?= $item->url() ?>'><?= $item->title()->html() ?></a>
            </li>
<?php endforeach ?>
<?php foreach ($items as $item): ?>
            <li<?php e($item->isActive(), ' class="current"') ?>>
              <a href='<?= $item->url() ?>'><?= $item->title()->html() ?></a>
            </li>
<?php endforeach ?>
<?php foreach ($items as $item): ?>
            <li<?php e($item->isOpen(), ' class="current"') ?>>
              <a href='<?= $item->url() ?>'><?= $item->title()->html() ?></a>
            </li>
<?php endforeach ?>

The difference between isActive() and isOpen() is that with isOpen() the menu item is still active when a subpage of the parent is active. While with isActive(), the menu item is only active when the page itself is active.

The two additional checks in your first example are redundant, because you already filter by listed status.

I have understood that as far as it goes. Only when I land on the home page after calling up the URL, the first page is marked as active although it is not the home page.

domain.de was called up and the page domain.de/about is shown as active. about is the first entry in the loop.

That is weird. Please have a look at the starterkit navigation. There it works exactly like it should.

I have now tested lines 80-82 of the header.php snippet from the StarterKit (starterkit/header.php at main · getkirby/starterkit · GitHub).

Unfortunately with the same result. If I manually add a menu entry to the start page and use isOpen(). Then it also works.

Code:

<ul class='main-menu__list'>
<li<?php e($page->isOpen(), ' class="current"') ?>><a href="<?= $site->url() ?>">Home</a></li>
<?php
        // main menu items
        $items = $site->children()->listed()->filterBy('bottomMenu', false);
        // only show the menu if items are available
        if($items->isNotEmpty()):
?>
<?php foreach ($items as $item): ?>
            <li<?php e($item->isOpen() && !$item->isHomeOrErrorPage(), ' class="current"') ?>>
              <a href='<?= $item->url() ?>'><?= $item->title()->html() ?></a>
            </li>
<?php endforeach ?>
<?php endif ?>
</ul>

Is there no solution? Without having to add the start page manually.

Currently I have the manual entry in the source code with

style="display:none;".

I didn’t know your problem was about adding the start page but about the first page always being active?

If you want the homepage in the list

$items = $site->children()->listed()->filterBy('bottomMenu', false)->add(page('home'));

I don’t want to display the homepage in the menu. Hence my problem. My query only contains the pages that have been/will be created by me. But when I am on the homepage, the first entry is marked as active.

domain.tld => is called and shows the first entry in the menu as active although it is not the homepage but domain.tld/about.

I assume you have either an error somewhere or some setting that leads to this issue.

To shortcut this, feel free to PM me a download link to your project

you got a pm

The current class is added even if you remove the class from the php code, it seems to be some code in your theme.js that adds this class.

If you don’t think everything, thanks for the tip.