<pre><code>{% set date = (date()).modify('first day of january 00:00:00') %}
{% for month in 1..12 %}
    {% set endOfMonth = clone(date).modify('last day of this month') %}
        {% set exists= craft.entries
            .postDate(['and', '&gt;=' ~ date.format('-.m-d H:i:s'), '&lt;=' ~ endOfMonth.format('Y-m-d H:i:s') ])
            .exists()
        %}

    {% do date.modify('first day of next month') %}
{% endfor %}
</code></pre>

<p>loop all months and check if there are entries in this month or not</p>

CC BY-SA 4.0
2018-07-06T13:09:03.780Z
I am not getting it.  I updated my question.  I can't seem to get this to pull only those months with entries.
Hellyar
CC BY-SA 4.0
2018-07-06T13:11:45.403Z
What doesn't work... "I don't get it" is not really helpful. My code loops all months and tries to fetch the corresponding entries where `postDate` is within this month, if entries with such a condition exists -> if the `exists` variable is true you can display the button, if `exists` is not true the month doesn't have entries
Robin Schambach
CC BY-SA 4.0
2018-07-04T06:20:47.843Z
2018-07-04T06:20:47.843Z
Robin Schambach
<p>I setup my archive using the Craft <a href="https://craftcms.com/support/entry-archive" rel="nofollow noreferrer">demo</a>.  I have a page for each year.</p>

<p>I use <a href="https://www.kunkalabs.com/mixitup/" rel="nofollow noreferrer">MixItUp</a>,(think Isotope) to allow a user to filter entries by month. There are simple hard coded buttons for each month.</p>

<p>How can I loop through the entries and create a button for only those months that have entries?   </p>

<p>Update.  I have a loop that pulls all the entries for a given year.  I then use MixItUp (think Isotope) to filter the entries by month.  </p>

<p>There is a button for each month that triggers MixItUp.  I want to make the creation of these buttons dynamic.  That is, I want to create a loop that generates a button for only those months that contain entries and create a class with the month's name that corresponds with the month in the class names of the entry cards.</p>

<p>For any given year, the loop would create 1-12 buttons.</p>

<pre><code>{% for month that has &gt; 0 entries %}

&lt;button type="button" class="btn btn-outline-secondary btn-lg"   data-mixitup-control data-filter=".February"&gt;Feb&lt;/button&gt;

{% endfor %}
</code></pre>

<p><a href="https://i.sstatic.net/qXwSH.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/qXwSH.png" alt="enter image description here"></a></p>

<p>Thanks</p>

<templating>
List only those months that have entries
# List only those months that have entries

(Asked by: username_0 on 2018-07-04)

I setup my archive using the Craft demo.  I have a page for each year.
I use MixItUp,(think Isotope) to allow a user to filter entries by month. There are simple hard coded buttons for each month.
How can I loop through the entries and create a button for only those months that have entries?   
Update.  I have a loop that pulls all the entries for a given year.  I then use MixItUp (think Isotope) to filter the entries by month.  
There is a button for each month that triggers MixItUp.  I want to make the creation of these buttons dynamic.  That is, I want to create a loop that generates a button for only those months that contain entries and create a class with the month's name that corresponds with the month in the class names of the entry cards.
For any given year, the loop would create 1-12 buttons.
{% for month that has > 0 entries %}

Feb

{% endfor %}


Thanks

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2018-07-04. Score: 1

{% set date = (date()).modify('first day of january 00:00:00') %}
{% for month in 1..12 %}
    {% set endOfMonth = clone(date).modify('last day of this month') %}
        {% set exists= craft.entries
            .postDate(['and', '>=' ~ date.format('-.m-d H:i:s'), '<=' ~ endOfMonth.format('Y-m-d H:i:s') ])
            .exists()
        %}

    {% do date.modify('first day of next month') %}
{% endfor %}

loop all months and check if there are entries in this month or not

> **Comments:**
> **username_0**: I am not getting it.  I updated my question.  I can't seem to get this to pull only those months with entries.  
> **username_1**: What doesn't work... "I don't get it" is not really helpful. My code loops all months and tries to fetch the corresponding entries where `postDate` is within this month, if entries with such a condition exists -> if the `exists` variable is true you can display the button, if `exists` is not true the month doesn't have entries  

---

