Answers.Body: <p>Assuming you have a Categories field associated with the entry, you can get all of the related categories in your template via <code>entry.myCategoriesField</code> (replace with the actual Categories field handle).</p>
  
  <p>So here’s how you would loop through all of them:</p>
  
  <pre><code>{% for category in entry.myCategoriesField %}
      &lt;li&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
  {% endfor %}
  </code></pre>
  
  <p>If you just want the last 2 categories, you can use Twig’s <a href="http://php.net/array_slice" rel="nofollow">slice</a> filter:</p>
  
  <pre><code>normal syntax:
  {% for category in entry.myCategoriesField|slice(-2) %}
  
  shortcut syntax:
  {% for category in entry.myCategoriesField[-2:] %}
  </code></pre>
  
  <p>If you want to set these categories to their own variables, so they can be referenced later on in your template, you could do this instead:</p>
  
  <pre><code>{% set parentCategory = entry.myCategoriesField.last() %}
  {% set grandparentCategory = parentCategory.parent() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-16T19:54:56.593Z
Answers.Text: Aha, got it - I'm such a noob! Wasn't aware I could use negative syntax with 'Slice' either. Thx.
Answers.UserDisplayName: Britchie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-16T22:16:43.573Z
Answers.Text: One further question regarding this: I can see how I can employ this for use in a breadcrumb, but if I wanted to assign the category titles (there's 2 of them) to separate variables so I can use them elsewhere in my template, how would I go about this? I know I can use "entry.myCatField.first()" but stuck on how to query the parent. TIA
Answers.UserDisplayName: Britchie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-16T23:15:08.913Z
Answers.Text: @Britchie Lots of ways to do this but I just updated my answer with what I think would be the simplest/most readable one.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-17T18:10:53.963Z
Answers.Text: Those are really useful snippets and should solve my issue - thx so much for the follow-up Brandon.
Answers.UserDisplayName: Britchie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-16T19:26:15.610Z
Answers.LastActivityDate: 2016-08-16T23:14:35.240Z
Answers.LastEditDate: 2016-08-16T23:14:35.240Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I have a pretty standard type entry page (<code>_entry.html</code>) which displays the details of an entry having navigated to it through 2 levels of categories. On this page I'd like to be able to query the two immediate category parents of this entry (sub-cat &amp; cat) for use in a breadcrumb and page title variables.</p>
  
  <p>I'm not sure how to approach this, or what the correct dot syntax is - I'm just getting up-to-speed with Craft CMS.</p>
  
  <p>Any tips/references?</p>
  
Tags: <categories>
Title: How to get parent categories from an entry?
ThreadText: # How to get parent categories from an entry?
  
  (Asked by: username_0 on 2016-08-16)
  
  I have a pretty standard type entry page (`_entry.html`) which displays the details of an entry having navigated to it through 2 levels of categories. On this page I'd like to be able to query the two immediate category parents of this entry (sub-cat & cat) for use in a breadcrumb and page title variables.
  
  I'm not sure how to approach this, or what the correct dot syntax is - I'm just getting up-to-speed with Craft CMS.
  
  Any tips/references?
  
  **Tags:** categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-16. Score: 3 | Accepted answer
  
  Assuming you have a Categories field associated with the entry, you can get all of the related categories in your template via `entry.myCategoriesField` (replace with the actual Categories field handle).
  
  So here’s how you would loop through all of them:
  
  ```
  {% for category in entry.myCategoriesField %}
      <li><a href="{{ category.url }}">{{ category.title }}</a></li>
  {% endfor %}
  
  ```
  
  If you just want the last 2 categories, you can use Twig’s [slice](http://php.net/array_slice) filter:
  
  ```
  normal syntax:
  {% for category in entry.myCategoriesField|slice(-2) %}
  
  shortcut syntax:
  {% for category in entry.myCategoriesField[-2:] %}
  
  ```
  
  If you want to set these categories to their own variables, so they can be referenced later on in your template, you could do this instead:
  
  ```
  {% set parentCategory = entry.myCategoriesField.last() %}
  {% set grandparentCategory = parentCategory.parent() %}
  
  ```
  
  > **Comments:**
  > **username_0**: Aha, got it - I'm such a noob! Wasn't aware I could use negative syntax with 'Slice' either. Thx.  
  > **username_0**: One further question regarding this: I can see how I can employ this for use in a breadcrumb, but if I wanted to assign the category titles (there's 2 of them) to separate variables so I can use them elsewhere in my template, how would I go about this? I know I can use "entry.myCatField.first()" but stuck on how to query the parent. TIA  
  > **username_1**: @username_0 Lots of ways to do this but I just updated my answer with what I think would be the simplest/most readable one.  
  > **username_0**: Those are really useful snippets and should solve my issue - thx so much for the follow-up username_1.  
  
  ---
  
  