4

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:

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

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?

darylknight
  • 3,290
  • 18
  • 42

2 Answers2

5

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 #}
    <ul>
        {% nav link in subMenu %}
            <li>
                <a href="{{ link.url }}">{{ link.title }}</a>
                {% ifchildren %}
                    <ul>
                        {% children %}
                    </ul>
                {% endifchildren %}
            </li>
        {% endnav %}
    </ul>

{% endif %}
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • 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 Aug 21 '14 at 13:28
1
{% set sidebarNav = entry.getChildren() %}

{% if sidebarNav | length %}

        <ul>
            {% for entry in sidebarNav %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% endfor %}
        </ul>
{% 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.

Mart
  • 58
  • 3
  • That doesn't output anything. By "All singles" I mean all the pages that need to appear in the navigation are Singles. – darylknight Aug 21 '14 at 11:32
  • 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 %} {{ entry.title }} {% endfor %}

    – Mart Aug 21 '14 at 12:12
  • @Mart that wouldn't work because the entries field to do the relations is added to the structure entries. – carlcs Aug 21 '14 at 12:47