Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T10:58:26.457Z
Answers.Text: 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.
Answers.UserDisplayName: Stuart Whitehead
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T10:40:52.483Z
Answers.Text: 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?
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-19T10:35:01.747Z
Answers.LastActivityDate: 2014-06-19T10:35:01.747Z
Answers.UserDisplayName: Stuart Whitehead
Body: <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>
  
Tags: <entries><categories>
Title: How to loop through category group and identify those related to entry
ThreadText: # 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 %}
      <a href="{{ cat.url }}" {% if cat.relatedTo(entry) %}class=active{% endif %}>{{ cat.title }}</a>
  {% 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 %}
      <a href="{{ cat.url }}" {% if cat.id in relatedCats %}class="active"{% endif %}>{{ cat.title }}</a>
  {% 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 username_0. 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.  
  
  ---
  
  