Documentation

Light-weight, fast, simple and powerful!
 
Translations of this page?:

Omitting Pages from Navigation

Here's a simple way to omit certain pages out of your navigation menu:

<ul>
<li<?php echo url_match('/') ? ' id="current"': null; ?>><a href="<?php echo URL_PUBLIC; ?>">Home</a></li>
<?php foreach($this->find('/')->children() as $menu): ?>
<?php
$omit_pages = array('somepageslug', 'someotherpageslug');
if (in_array($menu->slug, $omit_pages)) {
    continue;
}
?>
<li<?php echo (url_start_with($menu->url)) ? ' id="current"': null; ?>>
<?php echo $menu->link() . PHP_EOL; ?></li>
<?php endforeach; ?>
</ul>

Change the slug values in the $omit_pages array to match the slugs of the pages you want to omit from your navigation.

The same technique can be used with page id's instead, in this example pages 3, 6 and 9 are omitted:

<ul>
<li<?php echo url_match('/') ? ' id="current"': null; ?>><a href="<?php echo URL_PUBLIC; ?>">Home</a></li>
<?php foreach($this->find('/')->children() as $menu): ?>
<?php
$omit_pages = array(3,6,9);
if (in_array($menu->id, $omit_pages)) {
    continue;
}
?>
<li<?php echo (url_start_with($menu->url)) ? ' id="current"': null; ?>>
<?php echo $menu->link() . PHP_EOL; ?></li>
<?php endforeach; ?>
</ul>

Here's a function that does all of the above:

<?php
function snippet_sitemap($parent,$omit_pages=false)
{
    $out = '';
    $childs = $parent->children();
    if (count($childs) > 0)
    {
        $out = '<ul class="sitemap">' . PHP_EOL;
        foreach ($childs as $child)
    {
        if($child->getLoginNeeded() == Page::LOGIN_REQUIRED)
        {
        continue;
        }
 
if (in_array($child->id, $omit_pages)) {
    continue;
}
            $out .= '<li>'.$child->link().snippet_sitemap($child).'</li>' . PHP_EOL;
    }
        $out.= '</ul>' . PHP_EOL;
    }
    return $out . PHP_EOL;
}
 
// can be used for sitemap, navigation and such
// omit pages 3, 6 and 9
echo snippet_sitemap($this->find('/'),array(3,6,9));
?>
 
navbook/leave-out.txt · Last modified: 2012-01-29 14:27 by Fortron
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 wolfcms.org / design by yello studio / Wolf CMS Inside