Creating an archive page with full year and month list Trying to create an archive page for each of my blog types within Craft. The page sorts by year and by month. (year and month are coming from my routes in this code.)

By using the following, I can print out each month with posts and the number of posts the month has. My problem is that I also need to print out the months with no post count and can't seem to figure out how to do that.

{% set entriesInYear = craft.entries.section('pressReleases').limit(null).after(year).before(year+1) %}

{% for month, entries in entriesInYear | group("postDate|date('F')") %}
    &lt;li&gt;&lt;a href="#"&gt;{{ month }}&amp;nbsp;({{ entries|length }})&lt;/a&gt;&lt;/li&gt;
{% endfor %}


That will yield me a printout of:


April (7)
March (4)
February (1)


I just need to also print out all the months in between with a (0) indicator.
{% set groupedEntries = entriesInYear | group("postDate|date('F')") %}


Will give you an associative array where the keys are the months, and the values are the entries for that month.

Now you need to loop over all the months, not just the ones in the groupedEntries.

Make an array of all the months.

{% set allTheMonths = ['January', 'February', 'March', ... 'December'] %}


Make an array of all the months that have entries (twig's keys filter)

{% set monthsWithEntries = groupedEntries | keys %}


Loop over the months (all of them), and output either the number of entries (groupedEntries[month]|length) or 0

{% for month in allTheMonths %}
  {% if month in monthsWithEntries %}
    &lt;li&gt;&lt;a href="#"&gt;{{ month }}&amp;nbsp;({{ groupedEntries[month]|length }})&lt;/a&gt;&lt;/li&gt;
  {% else %}
    &lt;li&gt;&lt;a href="#"&gt;{{ month }}&amp;nbsp;(0)&lt;/a&gt;&lt;/li&gt;
  {% endif %}
{% endfor %}