There is not really a very simple way to do this. One thing you need to keep in mind is that channels
and structures
do not have an 'index' pagespage defined by default. Essentially there are no uri's for sectionschannels and structures as a whole, only for the entries within that sectionthem. You need to create these index pages yourself, using for example:
Singles
(one for each of your index pages), or- a
Structure
(using differentEntryTypes
for the different levels and/or page types), or - Template files (placed directly in your templates folder)
The other problem is that you may have channels that do not represent pages at all, but are just collections of data to be used elsewhereelsewhere; likewise, sections do not really have a way to define order. AsOverall not really a result, there is no good waymechanism to generate navigation by looping through the sectionsnav.
There are, however, other ways to organize your site to facilitate dynamic navigationdo this.
One solution is to use a dedicated Structure
whose purpose is to generate your sitespecifically for navigation (called 'primaryNav' for example) with a related entries field that links to other entries and a custom url field to link to other random pages as needed. You can then loop through that structure to generate your hierarchical nav.
Another option is to define all of your top level pages (what might normally be considered singles) within a single structure
using EntryTypes
to differentiate the various page types and define the unique field requirements of those pages (i.e. a structure called 'pages' for example). You can then loop through this structure to definegenerate your top level navigation. And usenav, using the EntryType
to determine which template code to display.
Either of these techniques can also be combinedused with other channels and or structures as needed to generate lower level pages where there are many of the same type (i.e. news, blog, articles, projects, etc.).
There are a lot of good examples of this on the exchange site. And you should alsoAlso have a look at craft's nav
tag in the documentation.