<p>I hope I have understand your problem..</p>

<p>Let's look at your code:</p>

<p><em>One sidenote: It might be good to limit your <code>pages</code>-selection only to active pages:</em></p>

<pre><code>{% set pages = craft.entries.section('about').level(1).status('live') %}
</code></pre>

<p>First of all, store the elements of the slug in an array:</p>

<pre><code>{% set slugs = craft.request.segments %}
</code></pre>

<h2>Here is the first for-loop for the first-level elements:</h2>

<pre><code>&lt;ul class="side-nav p-0"&gt;
{% for page in pages %}
    {% set activePage = (page.slug in slugs) %}
    &lt;li class="{% if activePage %} active{% endif %}"&gt;
        &lt;a href="{{page.url}}"&gt;{{ page.title }}&amp;nbsp;&lt;/a&gt;
 &lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
</code></pre>

<p>The important part here is to check whether the <strong>slug of the page in the for-loop</strong> is active. In your code, you checked the slug of the entry (of the currently active page, not the page in the loop) with the slug of the request. This will always yield a true result, which is what you experienced. </p>

<p>Using the code above you will by now receive a list of all first-level pages, the active one with labelled with <code>class="active"</code>.</p>

<h2>Now let's look into the second loop for the subpages.</h2>

<pre><code>{% set activeSubPage = (entry.level == 2 and entry.getParent().slug == page.slug) %}
{% if activePage or activeSubPage %}
    {% set subPage = craft.entries.descendantOf(page) %}
    &lt;ul class="p-0 pl-5"&gt;
        {% for child in subPage %}
            &lt;li&gt;&lt;a href="{{child.url}}"&gt;{{child.title}}&lt;/a&gt;&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
{% endif %}
</code></pre>

<p>This time it is not enough to check whether the slug of the active page in the loop matches the slug of the actual page (this will show children only if the parent page is selected), we also need to check whether the parent's slug matches the active page (to show children even if the active page is one of the children itself and not the parent).</p>

<p>With <code>activeSubPage</code> we check at first, whether the page is level 2 (therefore it has a parent and we won't run into an error for an undefined attribute on pages that are level 1 and do not have a parent) and if so, whether the parent's slug is equal to the slug of the current page in the loop. </p>

<p>We enclose the second for-loop with an if-clause that checks both of the results, whether the current item in the loop is either  <code>activePage</code> or <code>activeSubPage</code>. The remaining code is identical to your code. </p>

<p>You could now check again, whether the sub-item is active, by using the same principle: <code>{% if  (child.slug in slugs) %}active{% endif %}</code></p>

<p>Here is the complete updated code:</p>

<pre><code>{% set pages = craft.entries.section('about').level(1).status('live') %}
{% set slugs = craft.request.segments %}

&lt;ul class="side-nav p-0"&gt;
{% for page in pages %}
    {% set activePage = (page.slug in slugs) %}
    &lt;li class="{% if activePage %} active{% endif %}"&gt;
        &lt;a href="{{page.url}}"&gt;{{ page.title }}&amp;nbsp;&lt;/a&gt;
        {% set activeSubPage = (entry.level == 2 and entry.getParent().slug == page.slug) %}
        {% if activePage or activeSubPage %}
            {% set subPage = craft.entries.descendantOf(page) %}
            &lt;ul class="p-0 pl-5"&gt;
                {% for child in subPage %}
                    &lt;li&gt;&lt;a href="{{child.url}}"&gt;{{child.title}}&lt;/a&gt;&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        {% endif %}
 &lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
</code></pre>

<p>I hope this helps and solves your problem. If you have any questions or if you do not understand my explanation (sorry for my bad English, I'm not a native speaker): Don't hesitate to ask!</p>

<h2>Further improvements:</h2>

<p>With some small improvements you could put this code into a file and use it recursively for navigations with more than two levels. In your template, you would then be able to include it like this</p>

<pre><code>{% include "_includes/nav_links_withchildren" with { 'slug' : 'about' } %}
</code></pre>

<p>and would then include itself again to list sub-pages.</p>

CC BY-SA 4.0
2018-12-18T15:17:47.790Z
Thats exactly what I was looking for. Thank you for taking the time to form a well structured and understandable answer. I've learned something there and it works great! With regards to the improvements. Are you saying that I could drop an include containing this code and craft would pull the slug and then display the correct structure? I say this because I do have other Structures like this, but with different slugs obviously
Adam
CC BY-SA 4.0
2018-12-18T17:58:10.057Z
I am glad I could help. With regard to your question yes: You can put this code as it is into a file and include it. By the use of the `with`-part, you can also define a variable, let's call it "slug". You would now have to replace the `about` with `slug` and it should work. However, to use it recursively for more than the two layers, you would have to modify the code a little bit. I hope this helps.
Luke
CC BY-SA 4.0
2018-12-18T18:39:29.713Z
That's great, thank you. I will give that a try. Another thing I wanted to ask is if a level 2 child page is active, is there a way to add a class of active to the parent <li> of that child page too?
Adam
CC BY-SA 4.0
2018-12-18T22:41:48.123Z
It is already in there.. you can use the same if-clause that checks for subpages to do that.
Luke
CC BY-SA 4.0
2018-12-18T01:07:35.727Z
2018-12-18T01:07:35.727Z
Luke
<p>I'm currently setting up a Structure nav and am trying to set it up so that child pages only display if;</p>

<p>1) The child page itself (or it's sibling) is currently active
2) The parent page of that child is currently active</p>

<p>At the moment all I can get working is having the child pages display regardless of what page is active. Even if a page has no children, the child pages are still being shown beneath another parent page, whereas ideally those child pages would not be seen until those pages are become active.</p>

<pre><code>{% set pages = craft.entries.section('about').level('1') %}

&lt;ul class="side-nav p-0"&gt;
{% for page in pages %}
{% set activePage = (entry.slug == craft.request.segment(2)) %}
&lt;li class="{% if activePage %} active{% endif %}"&gt;
  &lt;a href="{{page.url}}"&gt;{{ page.title }}&amp;nbsp;&lt;/a&gt;
  {% if activePage %}
    {% set subPage = craft.entries.descendantOf(page) %}
    &lt;ul class="p-0 pl-5"&gt;
      {% for child in subPage %}
        &lt;li&gt;&lt;a href="{{child.url}}"&gt;{{child.title}}&lt;/a&gt;&lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;
   {% endif %}
 &lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Is there any solution out there for this as I feel like I maybe close to getting it working. Can anybody shed any light on this?</p>

<entries><craft3><structure><navigation><children>
Craft CMS Structure: How can I display parent and child items on an active parent page?
# Craft CMS Structure: How can I display parent and child items on an active parent page?

(Asked by: username_0 on 2018-12-17)

I'm currently setting up a Structure nav and am trying to set it up so that child pages only display if;
1) The child page itself (or it's sibling) is currently active
2) The parent page of that child is currently active
At the moment all I can get working is having the child pages display regardless of what page is active. Even if a page has no children, the child pages are still being shown beneath another parent page, whereas ideally those child pages would not be seen until those pages are become active.
{% set pages = craft.entries.section('about').level('1') %}


{% for page in pages %}
{% set activePage = (entry.slug == craft.request.segment(2)) %}

  {{ page.title }} 
  {% if activePage %}
    {% set subPage = craft.entries.descendantOf(page) %}
    
      {% for child in subPage %}
        {{child.title}}
      {% endfor %}
    
   {% endif %}
 
{% endfor %}


Is there any solution out there for this as I feel like I maybe close to getting it working. Can anybody shed any light on this?

**Tags:** entries, craft3, structure, navigation, children


---

## 1 answers

### Answer by username_1 on 2018-12-18. Score: 3 | Accepted answer

I hope I have understand your problem..
Let's look at your code:
One sidenote: It might be good to limit your pages-selection only to active pages:
{% set pages = craft.entries.section('about').level(1).status('live') %}

First of all, store the elements of the slug in an array:
{% set slugs = craft.request.segments %}

Here is the first for-loop for the first-level elements:

{% for page in pages %}
    {% set activePage = (page.slug in slugs) %}
    
        {{ page.title }} 
 
{% endfor %}


The important part here is to check whether the slug of the page in the for-loop is active. In your code, you checked the slug of the entry (of the currently active page, not the page in the loop) with the slug of the request. This will always yield a true result, which is what you experienced. 
Using the code above you will by now receive a list of all first-level pages, the active one with labelled with class="active".
Now let's look into the second loop for the subpages.
{% set activeSubPage = (entry.level == 2 and entry.getParent().slug == page.slug) %}
{% if activePage or activeSubPage %}
    {% set subPage = craft.entries.descendantOf(page) %}
    
        {% for child in subPage %}
            {{child.title}}
        {% endfor %}
    
{% endif %}

This time it is not enough to check whether the slug of the active page in the loop matches the slug of the actual page (this will show children only if the parent page is selected), we also need to check whether the parent's slug matches the active page (to show children even if the active page is one of the children itself and not the parent).
With activeSubPage we check at first, whether the page is level 2 (therefore it has a parent and we won't run into an error for an undefined attribute on pages that are level 1 and do not have a parent) and if so, whether the parent's slug is equal to the slug of the current page in the loop. 
We enclose the second for-loop with an if-clause that checks both of the results, whether the current item in the loop is either  activePage or activeSubPage. The remaining code is identical to your code. 
You could now check again, whether the sub-item is active, by using the same principle: {% if  (child.slug in slugs) %}active{% endif %}
Here is the complete updated code:
{% set pages = craft.entries.section('about').level(1).status('live') %}
{% set slugs = craft.request.segments %}


{% for page in pages %}
    {% set activePage = (page.slug in slugs) %}
    
        {{ page.title }} 
        {% set activeSubPage = (entry.level == 2 and entry.getParent().slug == page.slug) %}
        {% if activePage or activeSubPage %}
            {% set subPage = craft.entries.descendantOf(page) %}
            
                {% for child in subPage %}
                    {{child.title}}
                {% endfor %}
            
        {% endif %}
 
{% endfor %}


I hope this helps and solves your problem. If you have any questions or if you do not understand my explanation (sorry for my bad English, I'm not a native speaker): Don't hesitate to ask!
Further improvements:
With some small improvements you could put this code into a file and use it recursively for navigations with more than two levels. In your template, you would then be able to include it like this
{% include "_includes/nav_links_withchildren" with { 'slug' : 'about' } %}

and would then include itself again to list sub-pages.

> **Comments:**
> **username_0**: Thats exactly what I was looking for. Thank you for taking the time to form a well structured and understandable answer. I've learned something there and it works great! With regards to the improvements. Are you saying that I could drop an include containing this code and craft would pull the slug and then display the correct structure? I say this because I do have other Structures like this, but with different slugs obviously  
> **username_1**: I am glad I could help. With regard to your question yes: You can put this code as it is into a file and include it. By the use of the `with`-part, you can also define a variable, let's call it "slug". You would now have to replace the `about` with `slug` and it should work. However, to use it recursively for more than the two layers, you would have to modify the code a little bit. I hope this helps.  
> **username_0**: That's great, thank you. I will give that a try. Another thing I wanted to ask is if a level 2 child page is active, is there a way to add a class of active to the parent <li> of that child page too?  
> **username_1**: It is already in there.. you can use the same if-clause that checks for subpages to do that.  

---

