Trace: » accesskey » collapsing » inline-horizontal » leave-out » rolling-vertical » simple » sitemap-style » stacking-collapsing » styledlist » unlimited
Translations of this page?:
General
Getting & Giving Support
Administration
Design
- Tut. - Layouts and Themes
- Tut. - Navigation cookbook
- Tut. - How To section
Reference Material
Plugin Development
- Tut. - Writing a plugin
- Ref. - List of Events
- Ref. - Enabling cron jobs
- Ref. - PHPDoc
Core Development
- Ref. - PHPDoc
Press information
Wolf CMS books section
Recipe: Unlimited levels and children
This is a recursive function that allows the user to display an unlimited number of levels and children in a static menu. It also allows you to limit the number of children that are displayed for particular pages. This is achieved with an array of slug/limit pairs. The function's options are:
- required, The page from where you want to start displaying the menu;
- required, The current page;
- optional, If true, start with a <UL>
- optional, An array of slug/limit pairs where limit is the maximum number of children to display for that particular slug.
<?php function displayChildren($page, $current, $startmenu = true, $limits = null) { if ($limits != null && array_key_exists($page->slug, $limits)) { $arr = array('order' => 'position ASC, published_on DESC', 'limit' => $limits[$page->slug]); } else $arr = array('order' => 'position ASC, published_on DESC'); if ($page && count($page->children()) > 0) { echo ($startmenu) ? '<ul>' : ''; foreach($page->children($arr) as $menu) : echo '<li'.(in_array($menu->slug, explode('/', $current->url)) ? ' class="current"': null).'>'.$menu->link($menu->title); displayChildren($menu, $current, true, $limits); echo '</li>'; endforeach; echo ($startmenu) ? '</ul>' : ''; } } ?>
Just add the above function to a Snippet or Layout. Then you can use the following code on a page somewhere to produce the actual menu:
<h1>Menu</h1> <?php $page = $this->find('/'); echo '<ul class="sidemenu">'; echo '<li>'.$page->link($page->title, (in_array($page->slug, explode('/', $this->url)) ? ' class="current"': null)).'</li>'; echo displayChildren($page, $this, false, array('articles' => '3', 'a-sub-page' => '1')); echo '</ul>'; ?>
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
