<p>Your solution looks like it's almost there, but it would be making a lot of DB queries as Craft will be querying the database for each category. You can give Craft all the help you can with something like this.</p>

<pre><code>{# First, retrieve all of your events categories #}
{% set cats = craft.categories.group('events').find() %}

{# Get the IDs of all categories related to this entry #}
{% set relatedCats = craft.categories.group('events').relatedTo({sourceElement: entry}).ids() %}

{# Test if the current category ID is in the array of related category IDs #}
{% for cat in cats %}
    &lt;a href="{{ cat.url }}" {% if cat.id in relatedCats %}class="active"{% endif %}&gt;{{ cat.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-06-19T10:58:26.457Z
Yeah, exactly Clive. Relations between element types (like entries and categories) are stored in a DB table. When you call `.relatedTo()`, Craft has to build a query to search this. Because you're calling `.relatedTo()` within a loop, the database will be queried for every category you iterate over, and the queries can be quite big.
Stuart Whitehead
CC BY-SA 3.0
2014-06-19T10:40:52.483Z
Brilliant. Works a treat. I know cat.relatedTo(entry) wasn't working, but, if it was, why would that have meant more DB queries? Is it because entry.relatedTo(cat) would query the DB each time?
Clive Portman
CC BY-SA 3.0
2014-06-19T10:35:01.747Z
2014-06-19T10:35:01.747Z
Stuart Whitehead
<p>Looping through a category group to show links to all the categories. This is an entry page and I want to highlight any categories assigned to the entry. How do I do that? I've tried this so far:</p>

<pre><code>{% set cats = craft.categories.group('events').find() %}
{% for cat in cats %}
    &lt;a href="{{ cat.url }}" {% if cat.relatedTo(entry) %}class=active{% endif %}&gt;{{ cat.title }}&lt;/a&gt;
{% endfor %}
</code></pre>

<entries><categories>
How to loop through category group and identify those related to entry
# How to loop through category group and identify those related to entry

(Asked by: username_0 on 2014-06-19)

Looping through a category group to show links to all the categories. This is an entry page and I want to highlight any categories assigned to the entry. How do I do that? I've tried this so far:
{% set cats = craft.categories.group('events').find() %}
{% for cat in cats %}
    {{ cat.title }}
{% endfor %}

**Tags:** entries, categories


---

## 1 answers

### Answer by username_1 on 2014-06-19. Score: 9 | Accepted answer

Your solution looks like it's almost there, but it would be making a lot of DB queries as Craft will be querying the database for each category. You can give Craft all the help you can with something like this.
{# First, retrieve all of your events categories #}
{% set cats = craft.categories.group('events').find() %}

{# Get the IDs of all categories related to this entry #}
{% set relatedCats = craft.categories.group('events').relatedTo({sourceElement: entry}).ids() %}

{# Test if the current category ID is in the array of related category IDs #}
{% for cat in cats %}
    {{ cat.title }}
{% endfor %}

> **Comments:**
> **username_0**: Brilliant. Works a treat. I know cat.relatedTo(entry) wasn't working, but, if it was, why would that have meant more DB queries? Is it because entry.relatedTo(cat) would query the DB each time?  
> **username_1**: Yeah, exactly Clive. Relations between element types (like entries and categories) are stored in a DB table. When you call `.relatedTo()`, Craft has to build a query to search this. Because you're calling `.relatedTo()` within a loop, the database will be queried for every category you iterate over, and the queries can be quite big.  

---

