Site()-helper, highlighted navigation


As you know, I’m new to Kirby, HTML, CSS and PHP. :innocent: But I’m doing my homework. But sometimes I need your help…

I want to achieve, that the selected page is highlighted (mockup attached).

My header:



	<title><?= $site->title() ?> | <?= $page->title() ?></title>

	<?= css('assets/css/styles.css') ?>

	<header class="header-nav">

		<a href="<?= $site->url() ?>"><img src="assets/img/logo.png" alt="Das Logo dieser Webseite."></a>

			<?php foreach ($site->children()->listed() as $subpage): ?>
				<a href="<?= $subpage->url() ?>">
					<?= $subpage->title() ?>
			<?php endforeach ?>


This is my CSS-Snippet…

.active {
background-color: #A62C21;


…when I include that to my header-snipped in the a-brackets, I’ll get all my navigation-URLs in red. But I just want to set that active one to red. How can I do that?

Thank you in advance.

You can use the e() helper to test if the page is open, and if its true, add a class:

<a <?php e($subpage->isOpen(), ' class="active"') ?> href="<?= $subpage->url() ?>">
	<?= $subpage->title() ?>
1 Like

Thank you, that’s working fine. :slight_smile: Got a problem with my CSS (positioning the red box), but I’ll figure it out…