Documentation

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

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.

1) Read more about the url_start_with() function.
2) For more on page-parts and inheritance, see the content() function.
 
howto/display_a_list_of_subpages.txt · Last modified: 2011-09-12 00:46 (external edit)
 
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