1

I'd like to build a Structure that can be used to output the main site navigation; each "entry" in the Structure would have three custom fields (types): Category, Entries, URL

This question is close to what I want, except it's not using Nav; I want children, so I think I need Nav? Plus I couldn't get that code to work anyway.

Based on other questions and the docs, I have this:

{% set pages = craft.entries.section('navDropDownMenu').all() %}
<ul>
    {% nav page in pages %}
        <li>
            {% if page.entriesFieldLink %}
                <a href="{{ page.entriesFieldLink.first().url|default }}">{{ page.title }}</a>
            {% elseif page.urlFieldLink %}
                <a href="{{{{ page.urlFieldLink.first().url|default }}">{{ page.title }}</a>
            {% else %}
                <a href="{{ page.category.url }}">{{ page.title }}</a>
         {% endif %}
            {% ifchildren %}
                <ul>
                    {% children %}
                </ul>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>

The only thing that works here is the Category link, except it should be a child but it isn't. I'm new to Craft so I'm probably doing something basic wrong, but any help is appreciated.

2

I am doing something similar in using a Structure for the main navigation. Below is what I have. Hope it helps!

{% set menu = craft.entries.section('navMain').all() %}
<ul> 
{% nav link in menu %}
    <li>
    {% if link.menuEntry|length %}
        {% if craft.app.request.absoluteUrl ==  link.menuEntry[0].url %}
        <a href="{{ link.menuEntry[0].url }}" class="active">{{ link.title }}</a>
        {% else %}
            <a href="{{ link.menuEntry[0].url }}">{{ link.title }}</a>
         {% endif %}
    {% else %}
        {% if craft.app.request.absoluteUrl ==  link.menuCustomURL %}
            <a href="{{ link.menuCustomURL }}" class="active">{{ link.title }}</a>
        {% else %}
            <a href="{{ link.menuCustomURL }}">{{ link.title }}</a>
        {% endif %}
    {% endif %}
    {% ifchildren %}
        <ul>
            {% children %}
        </ul>
    {% endifchildren %}
    </li> 
{% endnav %}
</ul>
1
  • Nevermind, I had somehow removed the parent item. This DOES output the children correctly. Thanks! ---Thanks, this gets the links to output correctly (which I appreciate), but it doesn't structure the children properly (no sub UL for the children).--- Jan 29 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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