Answers.Body: <p>If you have a 2-level category, and you select one of the nested categories (<code>Los Angeles</code>), what you get in your entry is <code>Los Angeles</code> <strong>and</strong> its parent (<code>California</code>). There is no way to have <code>Los Angeles</code> without also <code>California</code>, though you can have <code>California</code> without <code>Los Angeles</code>. Anyway, that for loop <code>{% for place in entry.location %}</code> will loop over both of them.</p>
  
  <p>One way to be more explicit about the cities and the states is to loop over just the cities using <a href="https://craftcms.com/docs/templating/craft.categories#level" rel="nofollow">level</a> to fetch only the categories at a specific level in the category, and then for each city, fetch its ancestors with <a href="https://craftcms.com/docs/templating/craft.categories#ancestorOf" rel="nofollow">ancestorOf</a>. Your code would look like this:</p>
  
  <pre><code>{% for city in entry.location.level(2) %}
    {{city.title}},
    {% for state in craft.categories.ancestorOf(city) %}
      {{ state.title }}
    {% endfor %}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-22T20:32:53.683Z
Answers.Text: Excellent. Thanks Marion - this worked great! Appreciate it.
Answers.UserDisplayName: dace
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-22T20:14:11.350Z
Answers.LastActivityDate: 2016-07-22T20:14:11.350Z
Answers.UserDisplayName: Marion Newlevant
Body: <p>I have categories of cities and states set up as parents and children, for example:</p>
  
  <pre><code>California
    - Los Angeles
    - San Francisco
  
  New York
    - New York City
  </code></pre>
  
  <p>When I use:</p>
  
  <pre><code>{% for place in entry.location %}
    {{ place }}
  {% endfor %}
  </code></pre>
  
  <p>it returns to me the city state (for example: "Los Angeles California"). How can I separate out the parent and child? Ideally, I want to do something like this:</p>
  
  <pre><code>{{ place.child }}, {{ place.parent }}
  </code></pre>
  
  <p>Thanks.</p>
  
Tags: <categories>
Title: How to separate a parent and it's children when looping through a category?
ThreadText: # How to separate a parent and it's children when looping through a category?
  
  (Asked by: username_0 on 2016-07-22)
  
  I have categories of cities and states set up as parents and children, for example:
  
  ```
  California
    - Los Angeles
    - San Francisco
  
  New York
    - New York City
  
  ```
  
  When I use:
  
  ```
  {% for place in entry.location %}
    {{ place }}
  {% endfor %}
  
  ```
  
  it returns to me the city state (for example: "Los Angeles California"). How can I separate out the parent and child? Ideally, I want to do something like this:
  
  ```
  {{ place.child }}, {{ place.parent }}
  
  ```
  
  Thanks.
  
  **Tags:** categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-07-22. Score: 4 | Accepted answer
  
  If you have a 2-level category, and you select one of the nested categories (`Los Angeles`), what you get in your entry is `Los Angeles` **and** its parent (`California`). There is no way to have `Los Angeles` without also `California`, though you can have `California` without `Los Angeles`. Anyway, that for loop `{% for place in entry.location %}` will loop over both of them.
  
  One way to be more explicit about the cities and the states is to loop over just the cities using [level](https://craftcms.com/docs/templating/craft.categories#level) to fetch only the categories at a specific level in the category, and then for each city, fetch its ancestors with [ancestorOf](https://craftcms.com/docs/templating/craft.categories#ancestorOf). Your code would look like this:
  
  ```
  {% for city in entry.location.level(2) %}
    {{city.title}},
    {% for state in craft.categories.ancestorOf(city) %}
      {{ state.title }}
    {% endfor %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Excellent. Thanks username_1 - this worked great! Appreciate it.  
  
  ---
  
  