How to get parent categories from an entry? 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 &amp; 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?
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 %}
    &lt;li&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
{% endfor %}


If you just want the last 2 categories, you can use Twig’s 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() %}