Answers.Body: <p>If you're using the nav on a category page, this should do what you want:</p>
  
  <p>You need to name your two category variables differently, so that you can compare them in the <code>nav</code> loop. So let's get the currently displayed category using <code>getLastSegment()</code> and save the CategoryModel to a var <code>currentCategory</code>.</p>
  
  <pre><code>{% set categorySlug = craft.request.getLastSegment() %}
  {% set currentCategory = craft.categories.group('compositions').slug(categorySlug).first() %}
  </code></pre>
  
  <p>The CategoryModel that is returned from the loop we simply name <code>category</code>. Within the loop compare the IDs of the two CategoryModels.</p>
  
  <pre><code>{% nav category in craft.categories.group('biographySection') %}
      &lt;li&gt;
          {% set activeClass = category.id == currentCategory.id ? 'active' %}
          &lt;a href="{{ category.url }}" class="{{ activeClass }}"&gt;{{ category.title }}&lt;/a&gt;
      &lt;/li&gt;
  {% endnav %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-26T10:16:02.900Z
Answers.LastActivityDate: 2015-01-26T10:27:58.180Z
Answers.LastEditDate: 2015-01-26T10:27:58.180Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>If you're using the nav on an entries page, this is how you'd do that:</p>
  
  <p>You can check if an ElementCriteriaModel with the following criteria returns anything using the <code>length</code> filter. The ID of the entries must match your current entry's ID and they must be related to the currently looped category.</p>
  
  <pre><code>&lt;li&gt;
      {% set activeClass = craft.entries.id(entry.id).relatedTo(category)|length ? 'active' %}
      &lt;a href="{{ category.url }}" class="{{ activeClass }}"&gt;{{ category.title }}&lt;/a&gt;
  &lt;/li&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-25T23:24:24.287Z
Answers.LastActivityDate: 2015-01-26T10:29:12.250Z
Answers.LastEditDate: 2015-01-26T10:29:12.250Z
Answers.UserDisplayName: carlcs
Body: <p>I have a category page displaying only the entries associated with the respective category. I have added a dynamic sub nav for categories using the following:</p>
  
  <pre><code>&lt;ul&gt;
      {% nav category in craft.categories.group('biographySection') %}
          &lt;li&gt;
              &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
          &lt;/li&gt;
      {% endnav %}
  &lt;/ul&gt;
  </code></pre>
  
  <p>I need to add an active class on the active link. Can’t figure out the best way to do this. Any help is appreciated. </p>
  
  <p>Below is the code used to display the content from a selected category:</p>
  
  <pre><code>{% set bio = craft.entries.section('biographies').relatedTo(category).find() %}
  {% for entry in bio %}
      {{ entry.title }}
  {% endfor %}
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-25T23:26:35.777Z
Comments.Text: You're using this nav on an entries page, not a category page, right?
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-01-26T09:13:49.900Z
Comments.Text: I’m sorry. Using this on a category page.
Comments.UserDisplayName: Clint Tabone
Tags: <categories><navigation>
Title: Adding active class to category link
ThreadText: # Adding active class to category link
  
  (Asked by: username_0 on 2015-01-25)
  
  I have a category page displaying only the entries associated with the respective category. I have added a dynamic sub nav for categories using the following:
  
  ```
  <ul>
      {% nav category in craft.categories.group('biographySection') %}
          <li>
              <a href="{{ category.url }}">{{ category.title }}</a>
          </li>
      {% endnav %}
  </ul>
  
  ```
  
  I need to add an active class on the active link. Can’t figure out the best way to do this. Any help is appreciated.
  
  Below is the code used to display the content from a selected category:
  
  ```
  {% set bio = craft.entries.section('biographies').relatedTo(category).find() %}
  {% for entry in bio %}
      {{ entry.title }}
  {% endfor %}
  
  ```
  
  **Tags:** categories, navigation
  
  > **Comments:**
  > **username_1**: You're using this nav on an entries page, not a category page, right?  
  > **username_0**: I’m sorry. Using this on a category page.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-01-26. Score: 2 | Accepted answer
  
  If you're using the nav on a category page, this should do what you want:
  
  You need to name your two category variables differently, so that you can compare them in the `nav` loop. So let's get the currently displayed category using `getLastSegment()` and save the CategoryModel to a var `currentCategory`.
  
  ```
  {% set categorySlug = craft.request.getLastSegment() %}
  {% set currentCategory = craft.categories.group('compositions').slug(categorySlug).first() %}
  
  ```
  
  The CategoryModel that is returned from the loop we simply name `category`. Within the loop compare the IDs of the two CategoryModels.
  
  ```
  {% nav category in craft.categories.group('biographySection') %}
      <li>
          {% set activeClass = category.id == currentCategory.id ? 'active' %}
          <a href="{{ category.url }}" class="{{ activeClass }}">{{ category.title }}</a>
      </li>
  {% endnav %}
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2015-01-25. Score: 2
  
  If you're using the nav on an entries page, this is how you'd do that:
  
  You can check if an ElementCriteriaModel with the following criteria returns anything using the `length` filter. The ID of the entries must match your current entry's ID and they must be related to the currently looped category.
  
  ```
  <li>
      {% set activeClass = craft.entries.id(entry.id).relatedTo(category)|length ? 'active' %}
      <a href="{{ category.url }}" class="{{ activeClass }}">{{ category.title }}</a>
  </li>
  
  ```
  
  
  ---
  
  