Answers.Body: <p>Lots of different ways to skin the snake here but depending on how big your site site is, why create a separate structure section to begin with?</p>
  
  <p>For a recent build,  I created a "Pages" structure section and put most of the relevant content in there. For "feeds" of data, like a Gallery or News or Employees and what not, you definitely want to use separate Channel sections for those, though, and just pull in the relevant channel from your templates.</p>
  
  <p>Coming from WordPress, it always annoyed me having to do the 2-step of creating a page and then hooking it up to the navigation and I found my clients agreed. They like that approach of mirroring the site structure with how the navigation actually works on the site. </p>
  
  <p>That allows them (or you) to rearrange content very easily if necessary.  In the Pages Structure, I also put a separate lightswitch that allows them to hide or show that page in the navigation depending on how they're using... for example, a landing page or something that they don't want to go "live" with just yet.</p>
  
  <p>Code from that site (put this in your <code>_layout.html</code> file or whatever you're using as main site"shell") if you want to use it sitewide:</p>
  
  <pre><code>              {% macro addClass(t) %}
                   {%if t.level == 1 and t.slug != "news" %}has-dropdown{%endif%}
                   {%if t.slug == "login" %} hide-for-large-up{%endif%}
                {% endmacro %}
  
                {% set pages = craft.entries({
                               section: 'pages',entryType:'not landingPage'
                               }).showInNavigation('1').level('&lt;=2') 
                %}
  
            &lt;ul&gt;
              {% nav page in pages %}
  
                  &lt;li class="{{ _self.addClass(page)}}"&gt;
  
                      {% switch page.type %}
  
                      {%case "linkToPage" %}
                           &lt;a href="{{page.pageLink}}"&gt;{{page.title}}&lt;/a&gt;
  
                      {% default %}
                          {{ page.getLink() }}
                      {%endswitch%}
  
  
                      {% ifchildren %}
                          &lt;ul class="dropdown"&gt;
                              {% children %}
                          &lt;/ul&gt;
                      {% endifchildren %}
  
                  &lt;/li&gt;
  
  
              {% endnav %}
           &lt;/ul&gt;
  </code></pre>
  
  <p>I have an entry type called <code>linktoPage</code> which has a <code>pageLink</code> field on there (so the client can put in links to outside Craft install, for example their portal). There's some Foundation 5 presentation stuff in there (addClass) to get the top nav working. I hate sub navigation on drop down menus so I'm only telling Craft to spit out the first 2 levels there but to get multiple nested child levels like in your example just remove <code>.level('&lt;=2')</code>.</p>
  
  <p>Again, YMMV, especially on what content you have.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T07:51:18.900Z
Answers.Text: Hiya. Many thanks for the code and the insights there. All very welcomed. Luckily the client won't mind adding entries directly to the navigation structure and it (to me anyway) feels like it will be better this way. I'm just thinking that if you want to take an entry out of the navigation then you can't just delete it as it would be gone forever and the only other way would be to disable it in the Structure which seems a little messy in itself. I may be wrong on that though and there might be a way around that though? So many ways to do things. There's never just one is there? ;-)
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T17:59:16.213Z
Answers.Text: No doubt, there isn't just one solution! The lightswitch is there in the entry to disable it from showing up in the nav but to still keep it alive as a functioning entry. I've had clients come from WordPress where they actually do delete the entry/post and wonder why it's still shows up in the nav and then 404s.  So yeh, there is no one right answer. I see benefits to both approaches, depending on the site. Every client is different—as always, no matter how you do it, just make sure they know how to use whatever solution you provide.
Answers.UserDisplayName: RitterKnight
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T18:00:39.767Z
Answers.Text: Yep you're absolutely right there. Thank you again for the kind insight and code though. I'm sure it could well come in handy at some point and if not for me then I'm sure for others who are looking for this sort of thing. Many thanks.
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T01:49:49.713Z
Answers.LastActivityDate: 2015-05-29T01:49:49.713Z
Answers.UserDisplayName: RitterKnight
Answers.Body: <blockquote>
    <p>First up I'm not sure if I actually need to place any information into the Entry URL Format and Entry Template settings for the section</p>
  </blockquote>
  
  <p>Nope, you can probably just un-check the <em>Entries in this section have their own URLs</em> checkbox in the Section settings and don't worry about it – I'm guessing the nav items should only function as pointers to "actual" entries, and won't need their own URLs or template.</p>
  
  <blockquote>
    <p>[...] in my Structure section I have an Entries field which allows me to
    choose an entry. I first of all have no idea of how to spit out this
    as the link instead of the {{ page.getLink() }} code above.</p>
  </blockquote>
  
  <p>You can't use the <code>getLink()</code> function when the URL you want is the URL for the entry in the Entries field, and not the actual URL for the nav item (phew). You'll have to build the <code>&lt;a&gt;</code> tag yourself – something like this should work:</p>
  
  <pre><code>{% set pages = craft.entries.section('navigation') %}
  &lt;nav&gt;
      &lt;ul&gt;
          {% nav page in pages %}
          &lt;li&gt;
              &lt;a href="{{ page.singleEntrySelector.first().url|default }}"&gt;{{ page.title }}&lt;/a&gt;
              {% ifchildren %}
                  &lt;ul&gt;
                      {% children %}
                  &lt;/ul&gt;
              {% endifchildren %}
          &lt;/li&gt;
          {% endnav %}
      &lt;/ul&gt;
  &lt;/nav&gt;
  </code></pre>
  
  <p>The crucial part here is the <code>page.singleEntrySelector.first().url|default</code> part for the <code>&lt;a&gt;</code> tags's <code>href</code> attribute.</p>
  
  <p>It's important to realize that whenever you call an Entries attribute/field – such as <code>page. singleEntrySelector</code> in this case – Craft returns an instance of <a href="http://buildwithcraft.com/docs/templating/elementcriteriamodel" rel="nofollow">ElementCriteriaModel</a>, which essentially is an array-like object. That means that even if the Entries field only has 1 entry inside it, you can't call <code>page. singleEntrySelector.url</code> directly – you'll need to explicitly select an entry inside the model, either by looping over it, calling a specific index (<code>page. singleEntrySelector[0].url</code>) or use the <code>first()</code> method to return the first entry. This is true for Asset, Tags, User and Category fields as well – basically any fieldtype that returns one or more related elements. </p>
  
  <p>If you're wondering about the <code>|default</code> filter applied to the URL, that's there to prevent Craft from throwing an error if the <code>page</code> doesn't actually have an entry selected in the <code>singleEntrySelector</code> field. If you make the latter a required field, you can skip the <code>|default</code> part.</p>
  
  <blockquote>
    <p>[...] I'd like to have it so that when I'm on say Sub
    Service 1 that I can create a secondary navigation on the page which
    will show me everything from Services down to (in this example)
    Service 3 so pretty much a very standard secondary navigation output.</p>
  </blockquote>
  
  <p>To output a sub navigation containing all nav "descendants" of the current entry, you'll first need to find the relevant nav item using the current entry in a relation query. Then, you can use the <code>descendantOf()</code> method to build the <code>pages</code> array, using the nav item as the ancestor: </p>
  
  <pre><code>{% set navItem = craft.entries.section('navigation').relatedTo(entry).first() %}
  {% if navItem %}
  {% set pages = craft.entries.descendantOf(navItem) %}
  &lt;nav&gt;
      &lt;ul&gt;
          {% nav page in pages %}
              &lt;li&gt;
                  &lt;a href="{{ page.singleEntrySelector.first().url | default }}"&gt;{{ page.title }}&lt;/a&gt;
              ...
  {% endif %}
  </code></pre>
  
  <p>If you want the sub nav to display the <em>entire</em> sub tree for the current entry – not just the <em>descendents</em> – this is how that could look:</p>
  
  <pre><code>{# Get the related nav item for the current entry #}
  {% set navItem = craft.entries.section('navigation').relatedTo(entry).first() %}
  {% if navItem %}
      {# Get the top level item #}
      {% set topItem = navItem.getAncestors().level(1).first()|default(navItem) %}
      {# Build a "pages" array w/ the top item and its descendents (child entries) #}
  {% set pages = craft.entries.id([topItem.id]|merge(topItem.getDescendants().ids()|default([]))) %}
  &lt;nav&gt;
      &lt;ul&gt;
      ....
  </code></pre>
  
  <p><em>Note: If you use the same Entry for the Entries field multiple times in your Navigation Structure, results may vary. The examples above will pull the first nav item that has a relationship with the current entry, and disregard any others. However, a many-to-one relationship between the Navigation Structure's entries and the entries in the Entries field, with single sub tree output for the sub nav doesn't really compute – so I've disregarded that particular issue and assumed the relationship will always be one-to-one.</em></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T09:46:41.920Z
Answers.Text: Thanks for the nice, detailed answer, much appreciated. Actually I did end up getting the singleEntrySelector field to output the link however I did it like this - {{ page.singleEntrySelector.first().getLink() }}. Not sure if that's the right thing to do or not although it did indeed spit out the correct link that I was looking for. Don't really know what the difference here is though in doing it that way?
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T09:48:26.327Z
Answers.Text: Also trying out your last piece of code to create a subnavigation wouldn't work for me I'm afraid. I placed it on the page with the same <nav> code as you showed above in your first piece of code but I just end up getting the whole navigation being spat out again as opposed to just the items in that section. Not sure where I'm going wrong with that though?
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T09:50:32.420Z
Answers.Text: Actually I lie. I am getting no output at all. I had not renamed pages to subpages and so was still getting the output from the first piece of code. I am definitely not getting any output at all from a secondary navigation.
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T09:55:45.307Z
Answers.Text: Yeah actually the entry.id will be wrong in my example - you need to do a relational query to get the nav item ID from the related entry ID. I'll have to look at that later, I'm afraid.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T09:57:11.933Z
Answers.Text: No problem at all. Many thanks for all of this. I can most definitely wait for the answer. :-)
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T10:39:35.120Z
Answers.Text: Updated the answer now. Hopefully this will help you out :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T11:09:50.397Z
Answers.Text: Thanks for the update but I'm afraid it's still not working :-( If I spit out {{ navItem }} below where it has been set then it spits out the title of one of the nav items however two things here are that it is the first sub-navigation item entry which it fetches and secondly running the whole code nothing (no navigation at all) gets spat out to the page.
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T11:27:41.210Z
Answers.Text: Hmm I'm going to put together a quick video to show what I'm getting as this is really strange :-(
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T11:39:44.430Z
Answers.Text: Okay here is a quick movie showing what I'm getting here. http://cl.ly/bAaE
  I think I should mention that at the moment (can be seen by looking at the URLs in the location bar) that some of the nav items are utilising the same entries just to test this all out. I would have hoped that this shouldn't affect this from working though as I will need to add entries into more than one place at once possibly.
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T12:00:54.773Z
Answers.Text: Looking at what the subnav is spitting out I actually do think this has something to do with the fact that entries are being re-used within the navigation although there must be a way of doing this without that happening surely?
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T12:56:26.350Z
Answers.Text: Yeah, that's a problem - but really more of a design issue than anything. Technically, the above code could easily be adapted to support multiple relations, but if the goal is a single sub nav tree, how would you handle a situation where the same entry has multiple "parents"? Ie. if an entry called "My Entry" is placed within both Services and Galleries in the nav Structure, what should the sub nav for that entry display? I think you'll find yourself hitting a wall with that one no matter the approach.
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T14:20:27.470Z
Answers.Text: I guess I'm really not understanding using a Structure at all then as lots of people had mentioned using one to create a navigation. Spitting out {{ page.id }} shows me the IDs of each Structure entry. It is **these** that I would have thought would be possible to check for. The fact that I'm using an **Entries** field I would have thought shouldn't be affecting this though? Surely it must be possible to find the main parent nav id in some way? If I hard-code the ID into the twig template then the correct menu gets given so it must be possible to programatically do this I would have thought?
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T18:38:50.273Z
Answers.Text: Hmm I think possibly we've ;-) been looking at this all wrong maybe? The code above *(I'm guessing)* is finding the descendant items based upon that **singleEntrySelector** field that I have within my Structure section? If so then that is going to get messed up if I use the same entry more than once in my navigation so I guess what I'm saying is that we really need a way to use the Structure IDs and not the IDs of the linked entries? That surely must be possible otherwise I don't really see what use a Structure is if you can't output different parts of it like this?
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T20:44:32.033Z
Answers.Text: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/24231/discussion-between-mmikkel-and-mark-bowen).
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T22:40:25.807Z
Answers.Text: Hi mmikel. Not sure if you now get notifications from the chat now that you've left but have just one small issue left with the code in the chat. Posted what is happening in there. Hope you can help with the final hurdle :-)
Answers.UserDisplayName: Mark Bowen
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-28T09:38:30.470Z
Answers.LastActivityDate: 2015-05-30T14:07:01.850Z
Answers.LastEditDate: 2015-05-30T14:07:01.850Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>As some of you may have seen I've been asking on here and on Slack regarding the best way to go about creating a navigation on a site.</p>
  
  <p>As yet, nothing I have tried is working and any code that I've been given to try also hasn't worked. <em>(Thanks to everyone who has given code so far though)</em>.</p>
  
  <p>Therefore I have literally stripped down <strong>everything</strong> in the hope that someone might be able to tell me where I'm going wrong as I'm sure that something as simple as this <strong>shouldn't</strong> be as difficult.</p>
  
  <p>I have a completely fresh install of Craft 2 with the standard templates that come with Craft and I have then created a <strong>Structure</strong> called <em>(creatively enough)</em> <strong>Navigation</strong> of which the hierarchy can be seen below.</p>
  
  <p><code>Home
  Services
  ---- Service 1
  ---- Service 2
  --------- Sub Service 1
  ---- Service 3
  Galleries
  ---- Gallery 1
  ---- Gallery 2
  ............
  </code>
  The above is actually just an example but reflects the sort of items that could go into the site.</p>
  
  <p>Any of the items above could also be either an entry, a single or a template that has a channel on it and so on and so forth.</p>
  
  <p>First up I'm not sure if I actually need to place any information into the <strong>Entry URL Format</strong> and <strong>Entry Template</strong> settings for the section as I'm <em>guessing</em> these aren't maybe all that important as the section is literally just being used for creating navigation on the site. That said I did fill them out with standard information just in case.</p>
  
  <p>Next on the standard <strong>news/_entry.html</strong> template I placed this code :</p>
  
  <p><code>{% set pages = craft.entries.section('navigation') %}
  &lt;nav&gt;
      &lt;ul&gt;
          {% nav page in pages %}
              &lt;li&gt;
                  {{ page.getLink() }}
                  {% ifchildren %}
                      &lt;ul&gt;
                          {% children %}
                      &lt;/ul&gt;
                  {% endifchildren %}
              &lt;/li&gt;
          {% endnav %}
      &lt;/ul&gt;
  &lt;/nav&gt;
  </code></p>
  
  <p>This allows me to spit out the entire navigation which is good so far but in my Structure section I have an <strong>Entries</strong> field which allows me to choose an entry. I first of all have no idea of how to spit out this as the link instead of the <code>{{ page.getLink() }}</code> code above.</p>
  
  <p>My <strong>Entries</strong> field is called <strong>singleEntrySelector</strong> and I tried <code>{{ page.singleEntrySelector }}</code> to see if it would spit anything out but that didn't work and I got an error on the page.</p>
  
  <p>So I'm wondering how do I go about spitting out the content of the <strong>singleEntrySelector</strong> field so that my navigation items can point to the correct URL?</p>
  
  <p>Once that is working I'd like to have it so that when I'm on say <strong>Sub Service 1</strong> that I can create a secondary navigation on the page which will show me everything from <strong>Services</strong> down to <em>(in this example)</em> <strong>Service 3</strong> so pretty much a very standard secondary navigation output.</p>
  
  <p>That should <em>(I'm hoping)</em> allow me to do what I need here although I am definitely open to other ways of approaching this if I'm completely seeing things wrong here?</p>
  
  <p>Many thanks,</p>
  
  <p>Mark</p>
  
  <p>P.S. I'd also like to be able to in the end spit out the navigation using whatever HTML code I need to as I will be utilising this on a responsive site and so have no idea yet as to what code I will need to encapsulate it all in. Therefore if the <code>{% nav %}</code> tag isn't the right fit here in that it won't allow me to do that then I'd really appreciate being shown how to spit it out in some other way that will allow me to do just that.</p>
  
Tags: <structure><navigation><output>
Title: Navigation, Structure, Usage, WOES!
ThreadText: # Navigation, Structure, Usage, WOES!
  
  (Asked by: username_0 on 2015-05-28)
  
  As some of you may have seen I've been asking on here and on Slack regarding the best way to go about creating a navigation on a site.
  
  As yet, nothing I have tried is working and any code that I've been given to try also hasn't worked. *(Thanks to everyone who has given code so far though)*.
  
  Therefore I have literally stripped down **everything** in the hope that someone might be able to tell me where I'm going wrong as I'm sure that something as simple as this **shouldn't** be as difficult.
  
  I have a completely fresh install of Craft 2 with the standard templates that come with Craft and I have then created a **Structure** called *(creatively enough)* **Navigation** of which the hierarchy can be seen below.
  
  `Home
  Services
  ---- Service 1
  ---- Service 2
  --------- Sub Service 1
  ---- Service 3
  Galleries
  ---- Gallery 1
  ---- Gallery 2
  ............`
  The above is actually just an example but reflects the sort of items that could go into the site.
  
  Any of the items above could also be either an entry, a single or a template that has a channel on it and so on and so forth.
  
  First up I'm not sure if I actually need to place any information into the **Entry URL Format** and **Entry Template** settings for the section as I'm *guessing* these aren't maybe all that important as the section is literally just being used for creating navigation on the site. That said I did fill them out with standard information just in case.
  
  Next on the standard **news/\_entry.html** template I placed this code :
  
  `{% set pages = craft.entries.section('navigation') %}
  <nav>
  <ul>
  {% nav page in pages %}
  <li>
  {{ page.getLink() }}
  {% ifchildren %}
  <ul>
  {% children %}
  </ul>
  {% endifchildren %}
  </li>
  {% endnav %}
  </ul>
  </nav>`
  
  This allows me to spit out the entire navigation which is good so far but in my Structure section I have an **Entries** field which allows me to choose an entry. I first of all have no idea of how to spit out this as the link instead of the `{{ page.getLink() }}` code above.
  
  My **Entries** field is called **singleEntrySelector** and I tried `{{ page.singleEntrySelector }}` to see if it would spit anything out but that didn't work and I got an error on the page.
  
  So I'm wondering how do I go about spitting out the content of the **singleEntrySelector** field so that my navigation items can point to the correct URL?
  
  Once that is working I'd like to have it so that when I'm on say **Sub Service 1** that I can create a secondary navigation on the page which will show me everything from **Services** down to *(in this example)* **Service 3** so pretty much a very standard secondary navigation output.
  
  That should *(I'm hoping)* allow me to do what I need here although I am definitely open to other ways of approaching this if I'm completely seeing things wrong here?
  
  Many thanks,
  
  Mark
  
  P.S. I'd also like to be able to in the end spit out the navigation using whatever HTML code I need to as I will be utilising this on a responsive site and so have no idea yet as to what code I will need to encapsulate it all in. Therefore if the `{% nav %}` tag isn't the right fit here in that it won't allow me to do that then I'd really appreciate being shown how to spit it out in some other way that will allow me to do just that.
  
  **Tags:** structure, navigation, output
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-05-28. Score: 6 | Accepted answer
  
  > First up I'm not sure if I actually need to place any information into the Entry URL Format and Entry Template settings for the section
  
  Nope, you can probably just un-check the *Entries in this section have their own URLs* checkbox in the Section settings and don't worry about it – I'm guessing the nav items should only function as pointers to "actual" entries, and won't need their own URLs or template.
  
  > [...] in my Structure section I have an Entries field which allows me to
  > choose an entry. I first of all have no idea of how to spit out this
  > as the link instead of the {{ page.getLink() }} code above.
  
  You can't use the `getLink()` function when the URL you want is the URL for the entry in the Entries field, and not the actual URL for the nav item (phew). You'll have to build the `<a>` tag yourself – something like this should work:
  
  ```
  {% set pages = craft.entries.section('navigation') %}
  <nav>
      <ul>
          {% nav page in pages %}
          <li>
              <a href="{{ page.singleEntrySelector.first().url|default }}">{{ page.title }}</a>
              {% ifchildren %}
                  <ul>
                      {% children %}
                  </ul>
              {% endifchildren %}
          </li>
          {% endnav %}
      </ul>
  </nav>
  
  ```
  
  The crucial part here is the `page.singleEntrySelector.first().url|default` part for the `<a>` tags's `href` attribute.
  
  It's important to realize that whenever you call an Entries attribute/field – such as `page. singleEntrySelector` in this case – Craft returns an instance of [ElementCriteriaModel](http://buildwithcraft.com/docs/templating/elementcriteriamodel), which essentially is an array-like object. That means that even if the Entries field only has 1 entry inside it, you can't call `page. singleEntrySelector.url` directly – you'll need to explicitly select an entry inside the model, either by looping over it, calling a specific index (`page. singleEntrySelector[0].url`) or use the `first()` method to return the first entry. This is true for Asset, Tags, User and Category fields as well – basically any fieldtype that returns one or more related elements.
  
  If you're wondering about the `|default` filter applied to the URL, that's there to prevent Craft from throwing an error if the `page` doesn't actually have an entry selected in the `singleEntrySelector` field. If you make the latter a required field, you can skip the `|default` part.
  
  > [...] I'd like to have it so that when I'm on say Sub
  > Service 1 that I can create a secondary navigation on the page which
  > will show me everything from Services down to (in this example)
  > Service 3 so pretty much a very standard secondary navigation output.
  
  To output a sub navigation containing all nav "descendants" of the current entry, you'll first need to find the relevant nav item using the current entry in a relation query. Then, you can use the `descendantOf()` method to build the `pages` array, using the nav item as the ancestor:
  
  ```
  {% set navItem = craft.entries.section('navigation').relatedTo(entry).first() %}
  {% if navItem %}
  {% set pages = craft.entries.descendantOf(navItem) %}
  <nav>
      <ul>
          {% nav page in pages %}
              <li>
                  <a href="{{ page.singleEntrySelector.first().url | default }}">{{ page.title }}</a>
              ...
  {% endif %}
  
  ```
  
  If you want the sub nav to display the *entire* sub tree for the current entry – not just the *descendents* – this is how that could look:
  
  ```
  {# Get the related nav item for the current entry #}
  {% set navItem = craft.entries.section('navigation').relatedTo(entry).first() %}
  {% if navItem %}
      {# Get the top level item #}
      {% set topItem = navItem.getAncestors().level(1).first()|default(navItem) %}
      {# Build a "pages" array w/ the top item and its descendents (child entries) #}
  {% set pages = craft.entries.id([topItem.id]|merge(topItem.getDescendants().ids()|default([]))) %}
  <nav>
      <ul>
      ....
  
  ```
  
  *Note: If you use the same Entry for the Entries field multiple times in your Navigation Structure, results may vary. The examples above will pull the first nav item that has a relationship with the current entry, and disregard any others. However, a many-to-one relationship between the Navigation Structure's entries and the entries in the Entries field, with single sub tree output for the sub nav doesn't really compute – so I've disregarded that particular issue and assumed the relationship will always be one-to-one.*
  
  > **Comments:**
  > **username_0**: Thanks for the nice, detailed answer, much appreciated. Actually I did end up getting the singleEntrySelector field to output the link however I did it like this - {{ page.singleEntrySelector.first().getLink() }}. Not sure if that's the right thing to do or not although it did indeed spit out the correct link that I was looking for. Don't really know what the difference here is though in doing it that way?  
  > **username_0**: Also trying out your last piece of code to create a subnavigation wouldn't work for me I'm afraid. I placed it on the page with the same <nav> code as you showed above in your first piece of code but I just end up getting the whole navigation being spat out again as opposed to just the items in that section. Not sure where I'm going wrong with that though?  
  > **username_0**: Actually I lie. I am getting no output at all. I had not renamed pages to subpages and so was still getting the output from the first piece of code. I am definitely not getting any output at all from a secondary navigation.  
  > **username_2**: Yeah actually the entry.id will be wrong in my example - you need to do a relational query to get the nav item ID from the related entry ID. I'll have to look at that later, I'm afraid.  
  > **username_0**: No problem at all. Many thanks for all of this. I can most definitely wait for the answer. :-)  
  > **username_2**: Updated the answer now. Hopefully this will help you out :)  
  > **username_0**: Thanks for the update but I'm afraid it's still not working :-( If I spit out {{ navItem }} below where it has been set then it spits out the title of one of the nav items however two things here are that it is the first sub-navigation item entry which it fetches and secondly running the whole code nothing (no navigation at all) gets spat out to the page.  
  > **username_0**: Hmm I'm going to put together a quick video to show what I'm getting as this is really strange :-(  
  > **username_0**: Okay here is a quick movie showing what I'm getting here. http://cl.ly/bAaE I think I should mention that at the moment (can be seen by looking at the URLs in the location bar) that some of the nav items are utilising the same entries just to test this all out. I would have hoped that this shouldn't affect this from working though as I will need to add entries into more than one place at once possibly.  
  > **username_0**: Looking at what the subnav is spitting out I actually do think this has something to do with the fact that entries are being re-used within the navigation although there must be a way of doing this without that happening surely?  
  > **username_2**: Yeah, that's a problem - but really more of a design issue than anything. Technically, the above code could easily be adapted to support multiple relations, but if the goal is a single sub nav tree, how would you handle a situation where the same entry has multiple "parents"? Ie. if an entry called "My Entry" is placed within both Services and Galleries in the nav Structure, what should the sub nav for that entry display? I think you'll find yourself hitting a wall with that one no matter the approach.  
  > **username_0**: I guess I'm really not understanding using a Structure at all then as lots of people had mentioned using one to create a navigation. Spitting out {{ page.id }} shows me the IDs of each Structure entry. It is **these** that I would have thought would be possible to check for. The fact that I'm using an **Entries** field I would have thought shouldn't be affecting this though? Surely it must be possible to find the main parent nav id in some way? If I hard-code the ID into the twig template then the correct menu gets given so it must be possible to programatically do this I would have thought?  
  > **username_0**: Hmm I think possibly we've ;-) been looking at this all wrong maybe? The code above *(I'm guessing)* is finding the descendant items based upon that **singleEntrySelector** field that I have within my Structure section? If so then that is going to get messed up if I use the same entry more than once in my navigation so I guess what I'm saying is that we really need a way to use the Structure IDs and not the IDs of the linked entries? That surely must be possible otherwise I don't really see what use a Structure is if you can't output different parts of it like this?  
  > **username_2**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/24231/discussion-between-mmikkel-and-mark-username_0).  
  > **username_0**: Hi mmikel. Not sure if you now get notifications from the chat now that you've left but have just one small issue left with the code in the chat. Posted what is happening in there. Hope you can help with the final hurdle :-)  
  
  ---
  
  ### Answer by username_1 on 2015-05-29. Score: 1
  
  Lots of different ways to skin the snake here but depending on how big your site site is, why create a separate structure section to begin with?
  
  For a recent build, I created a "Pages" structure section and put most of the relevant content in there. For "feeds" of data, like a Gallery or News or Employees and what not, you definitely want to use separate Channel sections for those, though, and just pull in the relevant channel from your templates.
  
  Coming from WordPress, it always annoyed me having to do the 2-step of creating a page and then hooking it up to the navigation and I found my clients agreed. They like that approach of mirroring the site structure with how the navigation actually works on the site.
  
  That allows them (or you) to rearrange content very easily if necessary. In the Pages Structure, I also put a separate lightswitch that allows them to hide or show that page in the navigation depending on how they're using... for example, a landing page or something that they don't want to go "live" with just yet.
  
  Code from that site (put this in your `_layout.html` file or whatever you're using as main site"shell") if you want to use it sitewide:
  
  ```
                {% macro addClass(t) %}
                   {%if t.level == 1 and t.slug != "news" %}has-dropdown{%endif%}
                   {%if t.slug == "login" %} hide-for-large-up{%endif%}
                {% endmacro %}
  
                {% set pages = craft.entries({
                               section: 'pages',entryType:'not landingPage'
                               }).showInNavigation('1').level('<=2') 
                %}
  
            <ul>
              {% nav page in pages %}
  
                  <li class="{{ _self.addClass(page)}}">
  
                      {% switch page.type %}
  
                      {%case "linkToPage" %}
                           <a href="{{page.pageLink}}">{{page.title}}</a>
  
                      {% default %}
                          {{ page.getLink() }}
                      {%endswitch%}
  
                      {% ifchildren %}
                          <ul class="dropdown">
                              {% children %}
                          </ul>
                      {% endifchildren %}
  
                  </li>
  
              {% endnav %}
           </ul>
  
  ```
  
  I have an entry type called `linktoPage` which has a `pageLink` field on there (so the client can put in links to outside Craft install, for example their portal). There's some Foundation 5 presentation stuff in there (addClass) to get the top nav working. I hate sub navigation on drop down menus so I'm only telling Craft to spit out the first 2 levels there but to get multiple nested child levels like in your example just remove `.level('<=2')`.
  
  Again, YMMV, especially on what content you have.
  
  > **Comments:**
  > **username_0**: Hiya. Many thanks for the code and the insights there. All very welcomed. Luckily the client won't mind adding entries directly to the navigation structure and it (to me anyway) feels like it will be better this way. I'm just thinking that if you want to take an entry out of the navigation then you can't just delete it as it would be gone forever and the only other way would be to disable it in the Structure which seems a little messy in itself. I may be wrong on that though and there might be a way around that though? So many ways to do things. There's never just one is there? ;-)  
  > **username_1**: No doubt, there isn't just one solution! The lightswitch is there in the entry to disable it from showing up in the nav but to still keep it alive as a functioning entry. I've had clients come from WordPress where they actually do delete the entry/post and wonder why it's still shows up in the nav and then 404s.  So yeh, there is no one right answer. I see benefits to both approaches, depending on the site. Every client is different—as always, no matter how you do it, just make sure they know how to use whatever solution you provide.  
  > **username_0**: Yep you're absolutely right there. Thank you again for the kind insight and code though. I'm sure it could well come in handy at some point and if not for me then I'm sure for others who are looking for this sort of thing. Many thanks.  
  
  ---
  
  