I'm rewriting my blog to use Jekyll. Jekyll uses the Liquid templating language so it makes it a little more difficult to learn how to customize.
I'd like to group my list of blog posts by year. How would I write the Liquid code to be able to do this?
{% for post in site.posts %}
<li><!-- display post year here (but only once, per year) --></li>
<li>
<a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
Best Answer
It can be done with much, much less Liquid code than in the existing answers:
This will return exactly the HTML specified in your question:
However, this is not the optimal solution, because the year numbers are "only" list items as well.
It's not much more Liquid code to put the year into a headline and to begin a new
<ul>
for each year's posts:The generated HTML:
You can also group by month and year instead (so that the headlines are
February 2012
,January 2012
and so on).To do this, you just need to replace
date: "%Y"
(in the second line of both above examples) bydate: "%B %Y"
.(
%B
is the full month name, see the documentation)