<p>First you loop through the categories then use the category to loop through all the entries related to that category.</p>

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

CC BY-SA 3.0
2017-01-08T05:06:19.093Z
2017-01-08T05:13:31.510Z
2017-01-08T05:13:31.510Z
a-am
<p>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?</p>

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

<pre><code>    &lt;div class="margin-2"&gt;&lt;/div&gt;
        {% set category = craft.entries.seciont('posts') %}


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

<templating>
How can I create a loop to cycle thru each category and the the entries within each category?
# How can I create a loop to cycle thru each category and the the entries within each category?

(Asked by: username_0 on 2017-01-07)

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.   
    
        {% set category = craft.entries.seciont('posts') %}


        Aircraft
        
        
            {% for entry in category.relatedTo(craft.categories.slug('category1')) %}
            
                
                    
                        
                         {{ entry.title }} k
                    
                
            
            {% endfor %}

**Tags:** templating

---

## 1 answers

### Answer by username_1 on 2017-01-08. Score: 2 | Accepted answer

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 %}


---

