Answers.Body: <p>You can loop through your top level categories and check if descendants are available. Then collect the relevant category IDs that you can later use to filter the ElementCriteriaModel. </p>
  
  <pre><code>{# Define array before loop #}
  {% set bottomLevelCatIds = [] %}
  
  {# Get top level categories only #}
  {% set topLevelCats = craft.categories.group('compositions').level(1) %}
  
  {# Loop through categories and collect relevant IDs #}
  {% for topLevelCat in topLevelCats %}
  
      {# Check for child categories #}
      {% if craft.categories.descendantOf(topLevelCat).descendantDist('1')|length %}
  
          {# Merge child category IDs #}
          {% set 2ndLevelCatIds = craft.categories.descendantOf(topLevelCat).descendantDist('1').ids() %}
          {% set bottomLevelCatIds = bottomLevelCatIds|merge(2ndLevelCatIds) %}
  
      {% else %}
  
          {# Merge to level category ID #}
          {% set bottomLevelCatIds = bottomLevelCatIds|merge([topLevelCat.id]) %}
  
      {% endif %}
  
  {% endfor %}
  
  {# Get the bottom level category models #}
  {% set categories = craft.categories.id(bottomLevelCatIds) %} 
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T12:07:36.777Z
Answers.Text: Great, the index page works like a charm. As soon as I try to load a specific category slug Craft says:Only variables can be assigned to. Unexpected token "number" of value "2" ("name" expected). I assume I'd have to set my nav with categories IDs instead of slugs?
Answers.UserDisplayName: jjem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T13:03:43.637Z
Answers.Text: Sorry I don't understand what you are trying to do, @jjem. Where and how do you want to load a category slug?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T13:25:26.253Z
Answers.Text: Ok I'll try to be clear. My entries are all related to one or two categories. I have 3 templates for that section: index.html (displays all entries sorted by category). entry.html (displaying entries related to a specific category chosen in the nav). detail.html (each single entry is displayed with pre/next and loops through the entries under that specific category). I'm new to Craft and a bit confused about how I should achieve this. Maybe a structure would be better.
Answers.UserDisplayName: jjem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T14:04:52.213Z
Answers.Text: So the `index.html` now works like you want and you have links to your category indexes with URLs like `mysite.com/cat/mycatslug/` that should use your `entry.html` template, right? I'm not totally sure but if set up correctly (→ [see Docs article](http://buildwithcraft.com/docs/categories)), that template should give you access to a automatically set variable `category` that you can use to query your entries.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T14:07:30.643Z
Answers.Text: If not try this: `{% set category = craft.categories.group('compositions').slug('mycatslug').first() %}` to get category model with that slug.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T14:31:47.213Z
Answers.Text: trying to make that easier. I cut it down to 2 templates only. index.html and _entry.html. Index now sorts both ALL entries and entries related to a specific category if its URL is called. With that code: `{% set entries = craft.entries.relatedTo(category).order('dateYear','comp_opus','title').limit(null) %}
  {% for category in craft.categories.group('compositions').relatedTo(entries).level(2).slug(lastSeg) %}`. Only issue: entries related to more than one category make the page load all category where it belongs to....
Answers.UserDisplayName: jjem
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-10-10T11:37:02.637Z
Answers.LastActivityDate: 2014-10-10T11:37:02.637Z
Answers.UserDisplayName: carlcs
Body: <p>I have a 2 level category system. Some have children, some do not.
  How can I display entries related to the last level only?</p>
  
  <p>If a category has no children, it should show the entries related to the 1st level. And if there are children, it should show entries related to that child category only.</p>
  
  <p>The code below currently shows both levels, which I don't want. How can I tell Craft to look for the last (bottom) level only?</p>
  
  <pre><code>{% set entries = craft.entries.relatedTo(category).order('dateYear','comp_opus','title').limit(0) %}
  {% for category in craft.categories.group('compositions').relatedTo(entries) %}
      {% for entry in entries.relatedTo(category).limit(7) %}
          {# Do stuff #}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
Tags: <templating><categories>
Title: Display entries related to a category (only bottom level)
ThreadText: # Display entries related to a category (only bottom level)
  
  (Asked by: username_0 on 2014-10-09)
  
  I have a 2 level category system. Some have children, some do not.
  How can I display entries related to the last level only?
  
  If a category has no children, it should show the entries related to the 1st level. And if there are children, it should show entries related to that child category only.
  
  The code below currently shows both levels, which I don't want. How can I tell Craft to look for the last (bottom) level only?
  
  ```
  {% set entries = craft.entries.relatedTo(category).order('dateYear','comp_opus','title').limit(0) %}
  {% for category in craft.categories.group('compositions').relatedTo(entries) %}
      {% for entry in entries.relatedTo(category).limit(7) %}
          {# Do stuff #}
      {% endfor %}
  {% endfor %}
  
  ```
  
  **Tags:** templating, categories
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-10-10. Score: 2
  
  You can loop through your top level categories and check if descendants are available. Then collect the relevant category IDs that you can later use to filter the ElementCriteriaModel.
  
  ```
  {# Define array before loop #}
  {% set bottomLevelCatIds = [] %}
  
  {# Get top level categories only #}
  {% set topLevelCats = craft.categories.group('compositions').level(1) %}
  
  {# Loop through categories and collect relevant IDs #}
  {% for topLevelCat in topLevelCats %}
  
      {# Check for child categories #}
      {% if craft.categories.descendantOf(topLevelCat).descendantDist('1')|length %}
  
          {# Merge child category IDs #}
          {% set 2ndLevelCatIds = craft.categories.descendantOf(topLevelCat).descendantDist('1').ids() %}
          {% set bottomLevelCatIds = bottomLevelCatIds|merge(2ndLevelCatIds) %}
  
      {% else %}
  
          {# Merge to level category ID #}
          {% set bottomLevelCatIds = bottomLevelCatIds|merge([topLevelCat.id]) %}
  
      {% endif %}
  
  {% endfor %}
  
  {# Get the bottom level category models #}
  {% set categories = craft.categories.id(bottomLevelCatIds) %} 
  
  ```
  
  > **Comments:**
  > **username_0**: Great, the index page works like a charm. As soon as I try to load a specific category slug Craft says:Only variables can be assigned to. Unexpected token "number" of value "2" ("name" expected). I assume I'd have to set my nav with categories IDs instead of slugs?  
  > **username_1**: Sorry I don't understand what you are trying to do, @username_0. Where and how do you want to load a category slug?  
  > **username_0**: Ok I'll try to be clear. My entries are all related to one or two categories. I have 3 templates for that section: index.html (displays all entries sorted by category). entry.html (displaying entries related to a specific category chosen in the nav). detail.html (each single entry is displayed with pre/next and loops through the entries under that specific category). I'm new to Craft and a bit confused about how I should achieve this. Maybe a structure would be better.  
  > **username_1**: So the `index.html` now works like you want and you have links to your category indexes with URLs like `mysite.com/cat/mycatslug/` that should use your `entry.html` template, right? I'm not totally sure but if set up correctly (→ [see Docs article](http://buildwithcraft.com/docs/categories)), that template should give you access to a automatically set variable `category` that you can use to query your entries.  
  > **username_1**: If not try this: `{% set category = craft.categories.group('compositions').slug('mycatslug').first() %}` to get category model with that slug.  
  > **username_0**: trying to make that easier. I cut it down to 2 templates only. index.html and _entry.html. Index now sorts both ALL entries and entries related to a specific category if its URL is called. With that code: `{% set entries = craft.entries.relatedTo(category).order('dateYear','comp_opus','title').limit(null) %} {% for category in craft.categories.group('compositions').relatedTo(entries).level(2).slug(lastSeg) %}`. Only issue: entries related to more than one category make the page load all category where it belongs to....  
  
  ---
  
  