Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-08T05:06:19.093Z
Answers.LastActivityDate: 2017-01-08T05:13:31.510Z
Answers.LastEditDate: 2017-01-08T05:13:31.510Z
Answers.UserDisplayName: a-am
Body: <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>
  
Tags: <templating>
Title: How can I create a loop to cycle thru each category and the the entries within each category?
ThreadText: # 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.
  
  ```
      <div class="margin-2"></div>
          {% set category = craft.entries.seciont('posts') %}
  
          <h6>Aircraft</h6>
          <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>
                      </a>
                  </div>
              </div>
              {% 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 %}
  
  ```
  
  
  ---
  
  