<p>How a look at the <a href="http://buildwithcraft.com/docs/templating/entrymodel#isAncestorOf" rel="nofollow"><code>isAncestorOf()</code></a> method in the Docs. The given example code probably covers all you need:</p>

<pre><code>{% set expanded = entry is defined and page.isAncestorOf(entry) %}
&lt;li{% if expanded %} class="expanded"{% endif %}&gt;
</code></pre>

<p>To only address the direct parent, use <a href="http://buildwithcraft.com/docs/templating/entrymodel#isParentOf" rel="nofollow"><code>isParentOf()</code></a> instead.</p>

CC BY-SA 3.0
2014-10-10T10:17:05.653Z
2014-10-10T10:38:43.087Z
2014-10-10T10:38:43.087Z
carlcs
<p>If it works for you to use the URL segments for comparisons, I posted an answer on how to do that here: <a href="https://craftcms.stackexchange.com/a/2233/125">"How can I add a dynamic “active” CSS class to the navigation on any given page?"</a></p>

CC BY-SA 3.0
2014-10-10T10:18:18.263Z
Sorry, only read that relevant line at the bottom of your post after posting this. Hopefully that other answer I just gave is more helpful :)
carlcs
CC BY-SA 3.0
2014-10-10T09:58:35.963Z
2014-10-10T09:58:35.963Z
2017-04-13T12:51:00.160Z
carlcs
<p>If you have an Entries field type in your navigation structure <a href="http://craftcookbook.net/recipes/88" rel="nofollow">(like the more advanced example in this post on the craft cookbook</a>) you should be able to use the relatedTo( ) parameter to do the comparison instead of using the segment in a URL. </p>

<p>For example if you had an Entries Field with the handle "relatedEntry" in your navigation structure you could change your code from this:</p>

<pre><code>  &lt;li class="{% if craft.request.getSegment(1) == nav_item.nav_url %}active {% endif %}{% if nav_item.hasDescendants() %}more{% endif %}"&gt;
</code></pre>

<p>to this:</p>

<pre><code>{# Fetch the related navigation entry based on our current entry #}
{% set relatedToEntry = navigation.relatedTo(entry)%}

{# Check to see if the mainNavigation entry matches the current entry. Only compare it with the top level navigation items #}
{% set active = (relatedToEntry[0].slug == nav_item.slug and nav_item.hasDescendants) or (relatedToEntry[0].parent != "" and relatedToEntry[0].parent.slug == nav_item.slug) %}

{# display the class "active" if #}
&lt;li class="{% if active %}active {% endif %}{% if nav_item.hasDescendants() %}more{% endif %}"&gt;
&lt;a href="{{ nav_item.relatedEntry[0].url }}"&gt;{{ nav_item.title }}&lt;/a&gt;&lt;/li&gt;
</code></pre>

<p>Note: For some reason I had to compare the slugs (as opposed to the names) in order to get a positive match.</p>

CC BY-SA 3.0
2014-10-22T19:23:36.250Z
2014-10-23T13:38:41.960Z
2014-10-23T13:38:41.960Z
RhealPoirier
<p>So I have a two-level Navigation in a structure. What I want is to give the parent of my entry (if a parent exists) the active tag.</p>

<p>right now I'm doing that:</p>

<pre><code>{% for nav_item in navigation %}
  &lt;li class="{% if craft.request.getSegment(1) == nav_item.nav_url %}active {% endif %}{% if nav_item.hasDescendants() %}more{% endif %}"&gt;

    &lt;a href="{{nav_item.nav_url}}"&gt;{{nav_item.title}}&lt;/a&gt;

  {% if nav_item.hasDescendants() %}
    &lt;ul class="submenu"&gt;
      {% for child in nav_item.getChildren() %}
        &lt;li&gt;
            &lt;a href="{{child.nav_url}}"&gt;{{child.title}}&lt;/a&gt;
        &lt;/li&gt;
      {% endfor %}
    &lt;/ul&gt;
  {% endif %}
  &lt;/li&gt;
{% endfor %}
</code></pre>

<p>sadly most children don't share the first segment</p>

<structure><navigation>
How give parent in navigation-structure the active tag
# How give parent in navigation-structure the active tag

(Asked by: username_0 on 2014-10-10)

So I have a two-level Navigation in a structure. What I want is to give the parent of my entry (if a parent exists) the active tag.
right now I'm doing that:
{% for nav_item in navigation %}
  

    {{nav_item.title}}

  {% if nav_item.hasDescendants() %}
    
      {% for child in nav_item.getChildren() %}
        
            {{child.title}}
        
      {% endfor %}
    
  {% endif %}
  
{% endfor %}

sadly most children don't share the first segment

**Tags:** structure, navigation


---

## 3 answers

### Answer by username_1 on 2014-10-10. Score: 1

How a look at the isAncestorOf() method in the Docs. The given example code probably covers all you need:
{% set expanded = entry is defined and page.isAncestorOf(entry) %}


To only address the direct parent, use isParentOf() instead.


---

### Answer by username_1 on 2014-10-10. Score: 0

If it works for you to use the URL segments for comparisons, I posted an answer on how to do that here: ["How can I add a dynamic “active” CSS class to the navigation on any given page?"](https://craftcms.stackexchange.com/a/2233/125)

> **Comments:**
> **username_1**: Sorry, only read that relevant line at the bottom of your post after posting this. Hopefully that other answer I just gave is more helpful :)  

---

### Answer by username_2 on 2014-10-22. Score: 0

If you have an Entries field type in your navigation structure (like the more advanced example in this post on the craft cookbook) you should be able to use the relatedTo( ) parameter to do the comparison instead of using the segment in a URL. 
For example if you had an Entries Field with the handle "relatedEntry" in your navigation structure you could change your code from this:
  

to this:
{# Fetch the related navigation entry based on our current entry #}
{% set relatedToEntry = navigation.relatedTo(entry)%}

{# Check to see if the mainNavigation entry matches the current entry. Only compare it with the top level navigation items #}
{% set active = (relatedToEntry[0].slug == nav_item.slug and nav_item.hasDescendants) or (relatedToEntry[0].parent != "" and relatedToEntry[0].parent.slug == nav_item.slug) %}

{# display the class "active" if #}

{{ nav_item.title }}

Note: For some reason I had to compare the slugs (as opposed to the names) in order to get a positive match.


---

