<p>Don't use the <code>nav</code> tag. Assuming a structure called <code>pages</code>, try something like this:</p>

<pre class="lang-twig prettyprint-override"><code>{% set pages = craft.entries.section('pages').level(1).all() %}
&lt;ul&gt;
{% for page in pages %}
    &lt;li&gt;
        {{ page.title }}
        {% if entry is defined and entry.hasDescendants %}
            {% set children = page.getChildren() %}
            &lt;ul&gt;
            {% for child in children %}
                &lt;li&gt;{{ child.title }}&lt;/li&gt;
            {% endfor %}
            &lt;/ul&gt;
        {% endif %}
    &lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 4.0
2019-06-19T08:51:40.210Z
2019-06-19T08:51:40.210Z
Oli
<p>I have this structure section:</p>

<pre><code>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
</code></pre>

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

<p><strong>Example:</strong></p>

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

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

<pre><code>1.0 Nav point level 1
2.0 Nav Point level 1
3.0 Nav Point level 1
</code></pre>

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

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


{% if topLevel.hasDescendants %}

  &lt;nav class="subNav"&gt;

    &lt;ul&gt;

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

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

    &lt;/ul&gt;

  &lt;/nav&gt;
{% endif %}
</code></pre>

<p>I can't wrap my head around it.</p>

<navigation><children>
Show only children relative to current page
# Show only children relative to current page

(Asked by: username_0 on 2019-06-18)

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 %}

  

    

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

      {% nav page in subnav  %}
        
            {{ page.title }}
            {% if entry.hasDescendantes %} // this does not work at all.
              {% ifchildren %}
                {% children %}
              {% endifchildren %}
            {% endif %}
        
      {% endnav %}

    

  
{% endif %}

I can't wrap my head around it.

**Tags:** navigation, children

---

## 1 answers

### Answer by username_1 on 2019-06-19. Score: 2 | Accepted answer

Don't use the nav tag. Assuming a structure called pages, try something like this:
{% set pages = craft.entries.section('pages').level(1).all() %}

{% for page in pages %}
    
        {{ page.title }}
        {% if entry is defined and entry.hasDescendants %}
            {% set children = page.getChildren() %}
            
            {% for child in children %}
                {{ child.title }}
            {% endfor %}
            
        {% endif %}
    
{% endfor %}


---

