Excluding certain links from the menu #284
-
How can I exclude certain links from the menu? Original nav.php, where the menu is dynamically constructed: <nav class="navigation">
<ul>
<li>
<a<?= $site->is('home') ? ' class="current"' : ""; ?> href="<?= $url; ?>">
<?= i('Home'); ?>
</a>
</li>
<?php foreach ($links as $link): ?>
<li>
<a<?= $link->current ? ' class="current"' : ""; ?> href="<?= $link->link ?: $link->url; ?>">
<?= $link->title; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav> I've found two solutions that work, but i'm not sure if that's the right way to do it. <nav class="navigation">
<ul>
<?php
$navLinks = [
$url . '/home' => 'Home',
$url . '/blog' => 'Blog',
$url . '/contact' => 'Contact',
];
$currentUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$currentUrl = preg_replace('#([^:])//+#', '$1/', $currentUrl);
foreach ($navLinks as $url => $name) {
$url = preg_replace('#([^:])//+#', '$1/', $url);
$isCurrent = strpos($currentUrl, $url) === 0 ? ' class="current"' : '';
echo "<li$isCurrent><a href=\"$url\">$name</a></li>";
}
?>
</ul>
</nav> Second option: <nav class="navigation">
<ul>
<li><a href="<?= $url; ?>/home">Home</a></li>
<li><a href="<?= $url; ?>/blog">Blog</a></li>
<li><a href="<?= $url; ?>/contact">Contact</a></li>
</ul>
</nav> Is there a better way to do this? I don't know php. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can add custom property to a page to tell the loop that certain item need to be excluded. This is common example: ---
title: Exclude Me
description: Exclude this page from menu.
skip: true
...
Lorem ipsum dolor sit amet. Then in the navigation loop (the second line): <?php foreach ($links as $link): ?>
<?php if ($link->skip) continue; ?>
<li>
<a href="<?= $link->url; ?>">
<?= $link->title; ?>
</a>
</li>
<?php endforeach; ?> |
Beta Was this translation helpful? Give feedback.
You can add custom property to a page to tell the loop that certain item need to be excluded. This is common example:
Then in the navigation loop (the second line):