Forums/Tips & Tricks

Creating navigation based on the page hierarchy

Michael Slater
posted this on May 3, 2011, 15:11

The Webvanta menu system lets you manage what appears in the menu independently of the page hierarchy. Usually this is a benefit, but there are times when you just want to let the page hierarchy directly create the navigation.

Here's some sample code that will generate a nested, unordered list based on the page hierarchy, excluding certain pages:

<w:find url='/'>
 <ul>
   <li><a href="<w:url />"><w:default_label /></a> <!-- home page -->
     <w:children:each>
       <w:unless_url matches="system|webvanta">
         <li><a href="<w:url />"><w:default_label /></a>
           <w:if_children>
             <ul>
               <w:children:each>
                 <li><a href="<w:url />"><w:default_label /></a></li>
               </w:children:each>
             </ul>
           </w:if_children>
         </li>
       </w:unless_url>
     </w:children:each>
   </li>
 </ul>
</w:find>

This starts by setting the page context to the home page. All other pages are children of the home page.

The first link is for the home page itself. Then there's a loop that iterates over the children (top-level pages), and within that, a loop that iterates over the childrens' children (the sub-pages).

This would give you ALL of the pages, and there's going to be some that you don't want, so we've shown an example, using w:unless_url, of how you can exclude top-level pages with "system" or "webvanta" in the slug (using a vertical bar character to "or" together the different strings we want to match).