2

I'm new to Craft CMS. I have the following single level navigation code:

<ul>
  <li>
    <a href="/products" class="app-nav__primary">Products</a>
    <ul class="app-nav__secondary">
      {% for entry in craft.entries.section('products') %}
      <li><a href="{{ entry.url }}">{{ entry.title }}</a>
      </li>
      {% endfor %}
    </ul>
  </li>
</ul>

How would i adjust this to show a second level navigation in within the parent li? I'm looking for something like this:

<ul>
  <li>
    <a href="/products" class="app-nav__primary">Products</a>
    <ul class="app-nav__secondary">
      {% for entry in craft.entries.section('products') %}
      <li><a href="{{ entry.url }}">{{ entry.title }}</a>
        if (child) { '
          <li><a href="child.link">child.title</a>
        }
        </li>
        {% endfor %}
    </ul>
    </li>
</ul>

1 Answer 1

3

You could use the children tag:

{% set entries = craft.entries.section('products') %}
<ul>
    <li>
        <a href="/products" class="app-nav__primary">Products</a>
        <ul class="app-nav__secondary">
            {% nav entry in entries %}
                <li>
                    <a href="{{ entry.url }}">{{ entry.title }}</a>
                    {% ifchildren %}
                        <ul>
                            {% children %}
                        </ul>
                    {% endifchildren %}
                </li>
            {% endnav %}
        </ul>
    </li>
</ul>
0

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.