Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T10:17:05.653Z
Answers.LastActivityDate: 2014-10-10T10:38:43.087Z
Answers.LastEditDate: 2014-10-10T10:38:43.087Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T10:18:18.263Z
Answers.Text: 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 :)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T09:58:35.963Z
Answers.LastActivityDate: 2014-10-10T09:58:35.963Z
Answers.LastEditDate: 2017-04-13T12:51:00.160Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-22T19:23:36.250Z
Answers.LastActivityDate: 2014-10-23T13:38:41.960Z
Answers.LastEditDate: 2014-10-23T13:38:41.960Z
Answers.UserDisplayName: RhealPoirier
Body: <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>
  
Tags: <structure><navigation>
Title: How give parent in navigation-structure the active tag
ThreadText: # 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 %}
    <li class="{% if craft.request.getSegment(1) == nav_item.nav_url %}active {% endif %}{% if nav_item.hasDescendants() %}more{% endif %}">
  
      <a href="{{nav_item.nav_url}}">{{nav_item.title}}</a>
  
    {% if nav_item.hasDescendants() %}
      <ul class="submenu">
        {% for child in nav_item.getChildren() %}
          <li>
              <a href="{{child.nav_url}}">{{child.title}}</a>
          </li>
        {% endfor %}
      </ul>
    {% endif %}
    </li>
  {% 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()`](http://buildwithcraft.com/docs/templating/entrymodel#isAncestorOf) method in the Docs. The given example code probably covers all you need:
  
  ```
  {% set expanded = entry is defined and page.isAncestorOf(entry) %}
  <li{% if expanded %} class="expanded"{% endif %}>
  
  ```
  
  To only address the direct parent, use [`isParentOf()`](http://buildwithcraft.com/docs/templating/entrymodel#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](http://craftcookbook.net/recipes/88)) 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:
  
  ```
    <li class="{% if craft.request.getSegment(1) == nav_item.nav_url %}active {% endif %}{% if nav_item.hasDescendants() %}more{% endif %}">
  
  ```
  
  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 #}
  <li class="{% if active %}active {% endif %}{% if nav_item.hasDescendants() %}more{% endif %}">
  <a href="{{ nav_item.relatedEntry[0].url }}">{{ nav_item.title }}</a></li>
  
  ```
  
  Note: For some reason I had to compare the slugs (as opposed to the names) in order to get a positive match.
  
  
  ---
  
  