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?

9
  • 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
    Commented Aug 21, 2014 at 12:51
  • with "top level pages" you meant the singles, related to a top level entry from your structure, right?
    – carlcs
    Commented Aug 21, 2014 at 13:27
  • 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 Commented Aug 21, 2014 at 13:29
  • Yes you can! That's where entry types come into the game.
    – carlcs
    Commented Aug 21, 2014 at 13:31
  • Entry types let you use different fields, not different templates (as far as I can tell) Commented Aug 21, 2014 at 13:43

2 Answers 2

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 %}
1
  • 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. Commented Aug 21, 2014 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.

3
  • That doesn't output anything. By "All singles" I mean all the pages that need to appear in the navigation are Singles. Commented Aug 21, 2014 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 %} <a href="{{ entry.url }}">{{ entry.title }}</a> {% endfor %}
    – Mart
    Commented Aug 21, 2014 at 12:12
  • @Mart that wouldn't work because the entries field to do the relations is added to the structure entries.
    – carlcs
    Commented Aug 21, 2014 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.