2

I would like to know how I can create a page that contains all entries from a section e.g. 'news'. I want to divide the entries by their given category.

News (section)

Category 1:

  • entry in category 1
  • second entry in category 1
  • third entry in category 1

Category 2:

  • entry in category 2
  • second entry in category 2
  • third entry in category 2

etc.

Does anyone know the code for this? The categories are all in the same category group.

6

You can use .relatedTo(category)

{% for category in craft.categories.group('categoryName') %}
  <h2>{{category.title}}</h2>
  {% for entry in craft.entries.section('news').relatedTo(category) %}
    <a href="{{entry.url}}">{{entry.title}}</a><br>
  {% endfor %}
{% endfor %}

You can check for length if you don't want to show categories which aren't related to any entry.

See the relations documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.