<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>

CC BY-SA 3.0
2015-01-26T10:16:02.900Z
2015-01-26T10:27:58.180Z
2015-01-26T10:27:58.180Z
carlcs
<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>

CC BY-SA 3.0
2015-01-25T23:24:24.287Z
2015-01-26T10:29:12.250Z
2015-01-26T10:29:12.250Z
carlcs
<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>

CC BY-SA 3.0
2015-01-25T23:26:35.777Z
You're using this nav on an entries page, not a category page, right?
carlcs
CC BY-SA 3.0
2015-01-26T09:13:49.900Z
I’m sorry. Using this on a category page.
Clint Tabone
<categories><navigation>
Adding active class to category link
# 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:

    {% nav category in craft.categories.group('biographySection') %}
        
            {{ category.title }}
        
    {% endnav %}


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') %}
    
        {% set activeClass = category.id == currentCategory.id ? 'active' %}
        {{ category.title }}
    
{% 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.

    {% set activeClass = craft.entries.id(entry.id).relatedTo(category)|length ? 'active' %}
    {{ category.title }}


---

