<p>Try using:</p>

<pre><code>   {% set pageIsActive = (page.id == entry.id) %}
</code></pre>

<p>and then testing for that in your if statements.</p>

<p><a href="http://craftcookbook.net/recipes/388" rel="nofollow">This article</a> covers pretty much every eventuality when it comes to classes and navigation and should help you achieve the markup you are after.</p>

CC BY-SA 3.0
2015-08-13T14:21:34.890Z
2015-08-13T14:28:41.780Z
2015-08-13T14:28:41.780Z
j00lz
<p>Currently we're highlighting the active page in our navigation by having a structure of menu items with a "menuLink" field and comparing that to the current entry uri. Something like this:</p>

<pre><code>{% nav link in Menu %}
    &lt;li&gt;
        {% if link.menuLink|length %}
            &lt;a{% if link.slug == entry.uri %} class="active"{% endif %} href="{{ link.menuLink[0].url }}"&gt;{{ link.title }}&lt;/a&gt;
        {% else %}
            &lt;a href="{{ link.customUrl }}"&gt;{{ link.title }}&lt;/a&gt;
        {% endif %}
    &lt;/li&gt; 
{% endnav %}
</code></pre>

<p>However, this doesn't work for children of structures or channels. We could change the if statement to look for the entry.uri OR the entry.section as below but obviously that only works if the section/channel has the same name as the link.slug:</p>

<pre><code>{% if link.slug == entry.uri or link.slug == entry.section %} class="active"{% endif %}
</code></pre>

<p>I searched through the previous questions but all the answers to this problem seem to involve reading a section of the URL but we want to be able to control the URLs and have some structure children have a top level URL rather than a /structure-name/{slug}</p>

<p>If we could have a field on the structure itself to say which menu item to highlight then you could just say something like </p>

<pre><code>if entry.section.type == channel or structure then active menu item = entry.section.menuItem 
</code></pre>

<p>but you can't have a field on a structure/channel as far as I'm aware?</p>

<p>Does anyone know of a way of solving this problem without the use of URL segments?</p>

<structure><navigation><channels>
Set navigation menu item to active for child pages
# Set navigation menu item to active for child pages

(Asked by: username_0 on 2015-08-13)

Currently we're highlighting the active page in our navigation by having a structure of menu items with a "menuLink" field and comparing that to the current entry uri. Something like this:
{% nav link in Menu %}
    
        {% if link.menuLink|length %}
            {{ link.title }}
        {% else %}
            {{ link.title }}
        {% endif %}
     
{% endnav %}

However, this doesn't work for children of structures or channels. We could change the if statement to look for the entry.uri OR the entry.section as below but obviously that only works if the section/channel has the same name as the link.slug:
{% if link.slug == entry.uri or link.slug == entry.section %} class="active"{% endif %}

I searched through the previous questions but all the answers to this problem seem to involve reading a section of the URL but we want to be able to control the URLs and have some structure children have a top level URL rather than a /structure-name/{slug}
If we could have a field on the structure itself to say which menu item to highlight then you could just say something like 
if entry.section.type == channel or structure then active menu item = entry.section.menuItem 

but you can't have a field on a structure/channel as far as I'm aware?
Does anyone know of a way of solving this problem without the use of URL segments?

**Tags:** structure, navigation, channels

---

## 1 answers

### Answer by username_1 on 2015-08-13. Score: 1

Try using:
   {% set pageIsActive = (page.id == entry.id) %}

and then testing for that in your if statements.
This article covers pretty much every eventuality when it comes to classes and navigation and should help you achieve the markup you are after.


---

