So, I have a loop that populates all the entries related to a given category. See below. I have multiple categories. Right now, I use the same code for each category - swapping only the name of the category out (e.g, category1, category2 ...). How can I build a loop that avoids this?

My current template is as follows. I did not include the repetition for each category.

    <div class="margin-2"></div>
        {% set category = craft.entries.seciont('posts') %}

        <div class="sep red margin-1"></div>
        <div class="owl-carousel owl-thumbs">
            {% for entry in category.relatedTo(craft.categories.slug('category1')) %}
            <div class="thumb dark-overlay">
                <div class="photo">
                    <a href="{{entry.url}}">
                        <img src="{{ entry.postImage.first().getURL('thumbImage')}}" alt="" width="235" height="235">
                        <span class="info"><span class="big-excerpt"> {{ entry.title }} k</span></span>
            {% endfor %}

1 Answer 1


First you loop through the categories then use the category to loop through all the entries related to that category.

{% for category in craft.categories.group('categoryGroup') %}
  {% for post in craft.entries.section('posts').relatedTo(category) %}
    {# your markup #}
  {% endfor %}
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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