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

前端 未结 8 1294
無奈伤痛
無奈伤痛 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 04:49

    Did not much like the other answer so here's an alternative for you. Basic logic: Display year/month only if it "new":

    {% assign var currentYear = 0 %}
    {% assign var currentMonth = 0 %}
    {% for post in site.posts  %}
    {% capture year %}{{ post.date | date: "%Y" }}{% endcapture %}
    {% capture month %}{{ post.date | date: "%B" }}{% endcapture %}
    
    {% if currentYear != year %}
    

    {{ year }}

    {% assign var currentYear = year %} {% endif %} {% if currentMonth != month %}

    {{ month }}

    {% assign var currentMonth = month %} {% endif %}

    {{ post.title }}

    {% endfor %}

提交回复
热议问题