Answers.Body: <p>Have a look at Craft’s <a href="https://craftcms.com/docs/templating/nav" rel="nofollow noreferrer"><code>nav</code></a> tag, which allows to recursively iterate through
  an array of element models. Only output the category title if the category has no descendants.</p>
  
  <pre><code>{% nav category in categories %}
      {% if not category.getDescendants().total() %}
          &lt;span&gt;{{ category.title }}&lt;/span&gt;
      {% endif %}
  
      {% ifchildren %}
          {% children %}
      {% endifchildren %}
  {% endnav %}
  </code></pre>
  
  <p>Another option would be to write a self-calling <a href="https://twig.sensiolabs.org/doc/1.x/tags/macro.html" rel="nofollow noreferrer"><code>macro</code></a> to recursively traverse the tree.</p>
  
  <pre><code>{% macro getBottomMostElements(elements) %}
      {% import _self as self %}
  
      {% for element in elements %}
          {% if element.getDescendants().total() %}
              {{ self.getBottomMostElements(element.getDescendants(1)) }}
          {% else %}
              &lt;span&gt;{{ element.title }}&lt;/span&gt;
          {% endif %}
      {% endfor %}
  {% endmacro %}
  
  {% import _self as macros %}
  {{ macros.getBottomMostElements(categories.level(1)) }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-06T19:59:28.883Z
Answers.Text: I've tried the code above, but it doesn't give the result that I want. Even though the entry category has 4 items when I add it to the loop it loops through all the 7 original categories. 
  
  I'm still getting a flat array....
Answers.UserDisplayName: Beuschau
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-06T21:01:34.080Z
Answers.Text: You shouldn't add it to your loop but replace your loop with it. `categories` has to be an ElementCriteriaModel.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-07T12:42:57.693Z
Answers.Text: Still no luck @Beuschau?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-08T08:19:32.020Z
Answers.Text: No still no luck. 
  Please take a look at this image [http://imgur.com/a/tdghB](http://imgur.com/a/tdghB)
Answers.UserDisplayName: Beuschau
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-08T18:07:01.010Z
Answers.Text: I tested both of these snippets and they do exactly what you want, output categories that have no children. I am pretty sure you’re passing in the wrong data for `categories`, which has to be an ElementCriteriaModel prepared to query for categories. Try the snippet with `{% set categories = craft.categories %}` set and it will work. I see in your picture that you have the field in a Matrix block, so the `categories` var would need to be set with something like `{% set categories = block.myCategoriesFieldHandle %}`.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-09T19:25:21.033Z
Answers.Text: I am using block.categories in var
Answers.UserDisplayName: Beuschau
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-06T09:55:04.373Z
Answers.LastActivityDate: 2017-07-06T09:55:04.373Z
Answers.UserDisplayName: carlcs
Body: <p><a href="https://i.sstatic.net/U1eYf.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/U1eYf.jpg" alt="enter image description here"></a></p>
  
  <p>I need to get the last item that don't have a child. So in this case I need </p>
  
  <p>Mobiltelefoner<br>
  Ipad<br>
  Køleskab<br>
  Vaskemaskine</p>
  
  <p>How to I get the last item that don't have a child without knowing the amount of levels?</p>
  
Tags: <templating><categories>
Title: Loop through categories and get bottom-most category for each tree
ThreadText: # Loop through categories and get bottom-most category for each tree
  
  (Asked by: username_0 on 2017-07-06)
  
  [![enter image description here](https://i.sstatic.net/U1eYf.jpg)](https://i.sstatic.net/U1eYf.jpg)
  
  I need to get the last item that don't have a child. So in this case I need
  
  Mobiltelefoner  
  Ipad  
  Køleskab  
  Vaskemaskine
  
  How to I get the last item that don't have a child without knowing the amount of levels?
  
  **Tags:** templating, categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-07-06. Score: 1
  
  Have a look at Craft’s [`nav`](https://craftcms.com/docs/templating/nav) tag, which allows to recursively iterate through
  an array of element models. Only output the category title if the category has no descendants.
  
  ```
  {% nav category in categories %}
      {% if not category.getDescendants().total() %}
          <span>{{ category.title }}</span>
      {% endif %}
  
      {% ifchildren %}
          {% children %}
      {% endifchildren %}
  {% endnav %}
  
  ```
  
  Another option would be to write a self-calling [`macro`](https://twig.sensiolabs.org/doc/1.x/tags/macro.html) to recursively traverse the tree.
  
  ```
  {% macro getBottomMostElements(elements) %}
      {% import _self as self %}
  
      {% for element in elements %}
          {% if element.getDescendants().total() %}
              {{ self.getBottomMostElements(element.getDescendants(1)) }}
          {% else %}
              <span>{{ element.title }}</span>
          {% endif %}
      {% endfor %}
  {% endmacro %}
  
  {% import _self as macros %}
  {{ macros.getBottomMostElements(categories.level(1)) }}
  
  ```
  
  > **Comments:**
  > **username_0**: I've tried the code above, but it doesn't give the result that I want. Even though the entry category has 4 items when I add it to the loop it loops through all the 7 original categories.   I'm still getting a flat array....  
  > **username_1**: You shouldn't add it to your loop but replace your loop with it. `categories` has to be an ElementCriteriaModel.  
  > **username_1**: Still no luck @username_0?  
  > **username_0**: No still no luck.  Please take a look at this image [http://imgur.com/a/tdghB](http://imgur.com/a/tdghB)  
  > **username_1**: I tested both of these snippets and they do exactly what you want, output categories that have no children. I am pretty sure you’re passing in the wrong data for `categories`, which has to be an ElementCriteriaModel prepared to query for categories. Try the snippet with `{% set categories = craft.categories %}` set and it will work. I see in your picture that you have the field in a Matrix block, so the `categories` var would need to be set with something like `{% set categories = block.myCategoriesFieldHandle %}`.  
  > **username_0**: I am using block.categories in var  
  
  ---
  
  