General
Getting Support
Administration
Design
Wolf Reference
Plugin Development
Core Development
- Design documents
- Working guidelines
- References
Press, Graphics and Translations
Wolf CMS books section
Display a List of Subpages
This “howto” explains the ”simple submenu” code found in the Navigational Cookbook.
Wolf's children() function gives an array of all published child-pages to a given page: $this->children(). In PHP a foreach loop allows you to list each member of the array:
<?php foreach ($this->children() as $child): ?> ... <?php endforeach; ?>
Note the closing endforeach; statement.
Since we (usually) want a menu in a list form, this code is wrapped in <ul>…</ul> tags, here including an ID for CSS styling, and adding the <li>…</li> tags for each navigation item:
<ul id="subnav"> <?php foreach ($this->children() as $child): ?> <li>...</li> <?php endforeach; ?> </ul>
Focussing now on the <li>…</li> part of the code, the $child variable is the object we need to use to get the page link, using the link() function:
<li><?php echo $child->link(); ?></li>
That will be enough to provide the entire list of subpages. Normally, however, websites provide a means of highlighting the “current” page in the menu. To do this, we add some parameters to the link() function:
echo $child->link($child->title, (url_start_with($child->url) ? ' class="current"': null));
Here, $child->title provides the link text, while (url_start_with($child->url) sets a condition which, if true, will allow the class="current" CLASS to be added to the anchor tags.1)
Putting all that together, we have a simple unordered list which gives links for all of a page's child-pages:
<ul id="nav_sub"> <?php foreach ($this->children() as $child): ?> <li><?php echo $child->link($child->title, (url_start_with($child->url) ? ' class="current"': null)); ?></li> <?php endforeach; ?> </ul>
Use this code in a “sidebar” page part which has the inheritance set to “true”.2) This way, the submenu will appear on each child page, but $this->children() will still point to their common parent page.
For more navigation techniques, see the “recipes” offered in the Navigational Cookbook.
