<pre><code>{% set sidebarNav = entry.getChildren() %}

{% if sidebarNav | length %}

        &lt;ul&gt;
            {% for entry in sidebarNav %}
            &lt;li&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/li&gt;
            {% endfor %}
        &lt;/ul&gt;
{% endif %}
</code></pre>

<p>Although I'm not sure what you mean by all singles? Is this what you're after? It will return all children of the current structure page.</p>

CC BY-SA 3.0
2014-08-21T11:32:06.710Z
That doesn't output anything. By "All singles" I mean all the pages that need to appear in the navigation are Singles.
darylknight
CC BY-SA 3.0
2014-08-21T12:12:33.067Z
Are these singles related to each relevant structure page? If so could you do a loop through the related field to display them?

{% for entry in entry.relatedFieldName %}
 <a href="{{ entry.url }}">{{ entry.title }}</a>
{% endfor %}
Mart
CC BY-SA 3.0
2014-08-21T12:47:42.817Z
@Mart that wouldn't work because the entries field to do the relations is added to the structure entries.
carlcs
CC BY-SA 3.0
2014-08-21T10:24:19.497Z
2014-08-21T10:24:19.497Z
Mart
<p>You first have to get the corresponding entry in your "menu" structure and then build your navigation upon that item.</p>

<p>Use the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param"><code>relatedTo()</code></a> parameter for that. The <code>entry</code> entry model that you automatically get on that single template is the <code>targetElement</code> of that relation, but you should be fine using the less specific <code>element</code> property used with the short syntax <code>relatedTo(myEntryModel)</code>.</p>

<pre><code>{# Get the (one!) related entry from the "menu" structure #}
{% set menuItem = craft.entries.section('menu').relatedTo(entry).first() %}

{# Check if there are descendant entries #}
{% if menuItem.hasDescendants() %}

    {# Get entries to build the sub-menu #}
    {% set subMenu = craft.entries.descendantOf(menuItem) %}

    {# Output the sub menu #}
    &lt;ul&gt;
        {% nav link in subMenu %}
            &lt;li&gt;
                &lt;a href="{{ link.url }}"&gt;{{ link.title }}&lt;/a&gt;
                {% ifchildren %}
                    &lt;ul&gt;
                        {% children %}
                    &lt;/ul&gt;
                {% endifchildren %}
            &lt;/li&gt;
        {% endnav %}
    &lt;/ul&gt;

{% endif %}
</code></pre>

CC BY-SA 3.0
2014-08-21T13:28:10.970Z
Hey carlcs - thank you! That code worked perfectly (after I fixed the variable names). Makes perfect sense when you read the code, it's just knowing the correct syntax.
darylknight
CC BY-SA 3.0
2014-08-21T12:42:08.403Z
2014-08-21T13:39:53.263Z
2014-08-21T13:39:53.263Z
carlcs
<p>I've got a single structure menu for my entire site which consists of a title and a single related entry. I use that to output the main menu, which uses this code:</p>

<pre><code>&lt;section class="top-bar-section"&gt;
    &lt;ul class="right"&gt;
        {% nav link in craft.entries.section('menu') %}
        &lt;li class="{% if link.slug == craft.request.getSegment(1) %}active{% endif %} {% if link.hasDescendants() %}has-dropdown{% endif %}"&gt; {# Checks if              the page link is the same as the slug of current page #}
            &lt;a href="{{ link.relatedEntry[0].url }}"&gt;{{link.title}}&lt;/a&gt;
            {% ifchildren %}
            &lt;ul class="dropdown"&gt;
                {% children %}
            &lt;/ul&gt;
            {% endifchildren %}
        &lt;/li&gt;
        {% endnav %}
    &lt;/ul&gt;
&lt;/section&gt;
</code></pre>

<p>On the top level pages, in their template they have a sidebar which should output the menu items in that main section - all singles. Is there a way to do this or do I need to create separate Structures for each section?</p>

CC BY-SA 3.0
2014-08-21T12:51:23.857Z
Why don't use use the structure for the content also? I don't see the benefits of using a bunch of singles and then have to manage the structure of them in a separate structure section using entries fields.
carlcs
CC BY-SA 3.0
2014-08-21T13:27:21.710Z
with "top level pages" you meant the singles, related to a top level entry from your structure, right?
carlcs
CC BY-SA 3.0
2014-08-21T13:29:25.583Z
Practically every page on the site has a different layout - and as far as I can tell it's not possible to set different templates for entries in a Structure channel
darylknight
CC BY-SA 3.0
2014-08-21T13:31:15.717Z
Yes you can! That's where [entry types](http://buildwithcraft.com/help/entry-type-templates) come into the game.
carlcs
CC BY-SA 3.0
2014-08-21T13:43:01.697Z
Entry types let you use different fields, not different templates (as far as I can tell)
darylknight
CC BY-SA 3.0
2014-08-21T13:43:53.617Z
Hey code knight, you didn't click the link I provided!
carlcs
CC BY-SA 3.0
2014-08-21T13:47:34.377Z
Ahh ok I see - managed to miss that in the docs, thanks. Dynamic templates. Now my head is spinning with flexibility again ><
darylknight
CC BY-SA 3.0
2014-08-21T13:49:46.467Z
Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/16589/discussion-between-codeknight-and-carlcs).
darylknight
CC BY-SA 3.0
2014-08-21T14:59:03.360Z
codeknight, you could also use a combine approach like I outlined in this answer: http://craftcms.stackexchange.com/a/1474/125
carlcs
<structure>
How to output a structure sub-navigation of current page
# How to output a structure sub-navigation of current page

(Asked by: username_0 on 2014-08-21)

I've got a single structure menu for my entire site which consists of a title and a single related entry. I use that to output the main menu, which uses this code:

    
        {% nav link in craft.entries.section('menu') %}
         {# Checks if              the page link is the same as the slug of current page #}
            {{link.title}}
            {% ifchildren %}
            
                {% children %}
            
            {% endifchildren %}
        
        {% endnav %}
    


On the top level pages, in their template they have a sidebar which should output the menu items in that main section - all singles. Is there a way to do this or do I need to create separate Structures for each section?

**Tags:** structure
> **Comments:**
> **username_1**: Why don't use use the structure for the content also? I don't see the benefits of using a bunch of singles and then have to manage the structure of them in a separate structure section using entries fields.  
> **username_1**: with "top level pages" you meant the singles, related to a top level entry from your structure, right?  
> **username_0**: Practically every page on the site has a different layout - and as far as I can tell it's not possible to set different templates for entries in a Structure channel  
> **username_1**: Yes you can! That's where [entry types](http://buildwithcraft.com/help/entry-type-templates) come into the game.  
> **username_0**: Entry types let you use different fields, not different templates (as far as I can tell)  
> **username_1**: Hey code knight, you didn't click the link I provided!  
> **username_0**: Ahh ok I see - managed to miss that in the docs, thanks. Dynamic templates. Now my head is spinning with flexibility again ><  
> **username_0**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/16589/discussion-between-codeknight-and-carlcs).  
> **username_1**: codeknight, you could also use a combine approach like I outlined in this answer: http://craftcms.stackexchange.com/a/1474/125  

---

## 2 answers

### Answer by username_1 on 2014-08-21. Score: 5 | Accepted answer

You first have to get the corresponding entry in your "menu" structure and then build your navigation upon that item.
Use the relatedTo() parameter for that. The entry entry model that you automatically get on that single template is the targetElement of that relation, but you should be fine using the less specific element property used with the short syntax relatedTo(myEntryModel).
{# Get the (one!) related entry from the "menu" structure #}
{% set menuItem = craft.entries.section('menu').relatedTo(entry).first() %}

{# Check if there are descendant entries #}
{% if menuItem.hasDescendants() %}

    {# Get entries to build the sub-menu #}
    {% set subMenu = craft.entries.descendantOf(menuItem) %}

    {# Output the sub menu #}
    
        {% nav link in subMenu %}
            
                {{ link.title }}
                {% ifchildren %}
                    
                        {% children %}
                    
                {% endifchildren %}
            
        {% endnav %}
    

{% endif %}

> **Comments:**
> **username_0**: Hey carlcs - thank you! That code worked perfectly (after I fixed the variable names). Makes perfect sense when you read the code, it's just knowing the correct syntax.  

---

### Answer by username_2 on 2014-08-21. Score: 1

{% set sidebarNav = entry.getChildren() %}

{% if sidebarNav | length %}

        
            {% for entry in sidebarNav %}
            {{ entry.title }}
            {% endfor %}
        
{% endif %}

Although I'm not sure what you mean by all singles? Is this what you're after? It will return all children of the current structure page.

> **Comments:**
> **username_0**: That doesn't output anything. By "All singles" I mean all the pages that need to appear in the navigation are Singles.  
> **username_2**: Are these singles related to each relevant structure page? If so could you do a loop through the related field to display them?  {% for entry in entry.relatedFieldName %}  <a href="{{ entry.url }}">{{ entry.title }}</a> {% endfor %}  
> **username_1**: @Mart that wouldn't work because the entries field to do the relations is added to the structure entries.  

---

