Jekyll: Group posts by year
Recently, I changed the layout of the home page of this blog to group posts by year rather than having one long list of posts. I had to spend some time looking this up, but it turns out Jekyll has a way to group an array by using an expression.
{% assign years = site.posts
| group_by_exp: "post", "post.date | date: '%Y'"
%}
Now you can use this grouped array to iterate over years like this:
{% for year in years %}
<p>{{ year.name }}</p>
<ul>
{% for post in year.items %}
<li>
<a class='title' href='{{ post.url }}'>{{ post.title }}</a>
</li>
{% endfor %}
</ul>
{% endfor %}
Here’s a screenshot of the home page of this blog with the new and old versions:
Links
Tags: jekyll