Clive Portman: code

Archives by year using Craft CMS

Output the navigation like so:

{% set allEntries = craft.entries.section('blog').limit(null) %}
{% for year, entriesInYear in allEntries | group("postDate.year") %}
{{ year }} ({{ entriesInYear | length }})
{% endfor %}

Set a route within Craft like which looks like blog/archive/year and choose a template. In the chosen template, set some vars:

{% if year is not defined %}
{% set year = now.year %}
{% endif %}
{% set dateRange = 'and, >= ' ~ year ~ '-01-01, < ' ~ year ~ '-12-31' %}

Then, display the entries:

{% set entries = craft.entries.section('blog').postDate(dateRange).limit(NULL) %}
{% for entry in entries %}
{{ entry.title }}
{% endfor %}

Smashed it.