Jekyll/Liquid Templating: How to group blog posts by year?

前端 未结 8 1295
無奈伤痛
無奈伤痛 2020-11-28 04:45

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

8条回答
  •  青春惊慌失措
    2020-11-28 05:06

    Some solutions above are very complex but then as @Trevor pointed out that we can levarage Jekyll's group_by_exp filter. Also I liked the solution but what I needed was grouped by Year and then inside that list grouped by Month. So, I tweaked it a little bit.

    {% assign postsByYear = site.posts | group_by_exp:"post", "post.date | date: '%Y'" %}
        {% for year in postsByYear %}
          

    {{ year.name }}

    {% assign postsByMonth = year.items | group_by_exp:"post", "post.date | date: '%B'" %} {% for month in postsByMonth %}

    {{ month.name }}

    {% endfor %} {% endfor %}

提交回复
热议问题