2

I have this structure section:

1.0 Nav point level 1
  1.1 Nav point level 2
  1.2 Nav point level 2
  1.3 Nav point level 2
2.0 Nav Point level 1
3.0 Nav Point level 1

I want to show only top level points as long as there is no children ont the current page.

Example:

When im on page 1.0 Nav point level 1 the navi shoud look like above (Because the current page has descendants).

But when im on 2.0 Nav Point level 1 the navi should look like this: (Everything below level 1 not visible because the current page has no children.)

1.0 Nav point level 1
2.0 Nav Point level 1
3.0 Nav Point level 1

I tried to wrap the children tag into a conditional. But that's not allowed:

{% set topLevel = (entry.level == 1) ? entry : entry.getAncestors().level(1).first()  %}


{% if topLevel.hasDescendants %}

  <nav class="subNav">

    <ul>

      {% set subnav =  topLevel.getDescendants()  %}

      {% nav page in subnav  %}
        <li>
            <a href="{{ page.url }}">{{ page.title }}</a>
            {% if entry.hasDescendantes %} // this does not work at all.
              {% ifchildren %}
                <ul>{% children %}</ul>
              {% endifchildren %}
            {% endif %}
        </li>
      {% endnav %}

    </ul>

  </nav>
{% endif %}

I can't wrap my head around it.

1

Don't use the nav tag. Assuming a structure called pages, try something like this:

{% set pages = craft.entries.section('pages').level(1).all() %}
<ul>
{% for page in pages %}
    <li>
        {{ page.title }}
        {% if entry is defined and entry.hasDescendants %}
            {% set children = page.getChildren() %}
            <ul>
            {% for child in children %}
                <li>{{ child.title }}</li>
            {% endfor %}
            </ul>
        {% endif %}
    </li>
{% endfor %}
</ul>
| improve this answer | |

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.