2

I am attempting to replicate the example from https://docs.craftcms.com/v3/templating/filters.html#group

However, I am unable to access the {{ year }} in the first for loop inside the h2 element. It just shows up as an empty string. Anyone have an idea whats going on?

Here is my code:

{% set allEntries = craft.entries.section('weeklyDigests').all() %}
{% set allEntriesByYear = allEntries|group('postDate.year') %}

{% for year, entriesInYear in allEntriesByYear %}
   <h2>{{ year }}</h2>

   <ul>

   {% for entry in entriesInYear %}
    <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
   {% endfor %}

   </ul>
{% endfor %}
1

You were close! In Craft 3, postDate is a full PHP DateTime object, so it'd need to be something like:

{% set allEntriesByYear = allEntries|group('postDate|date("Y")') %}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.