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

前端 未结 8 1312
無奈伤痛
無奈伤痛 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:13

    If you want to break it down by year, here's the code:

    {% for post in site.posts  %}
        {% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
        {% capture next_year %}{{ post.previous.date | date: "%Y" }}{% endcapture %}
    
        {% if forloop.first %}
        

    {{this_year}}

    {% else %} {% if this_year != next_year %}

{{next_year}}

    {% endif %} {% endif %} {% endfor %}

    If you want to break it down to year and months it can be achieved like this:

    {% for post in site.posts  %}
        {% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
        {% capture this_month %}{{ post.date | date: "%B" }}{% endcapture %}
        {% capture next_year %}{{ post.previous.date | date: "%Y" }}{% endcapture %}
        {% capture next_month %}{{ post.previous.date | date: "%B" }}{% endcapture %}
    
        {% if forloop.first %}
        

    {{this_year}}

    {{ this_month }}

    {% else %} {% if this_year != next_year %}

{{next_year}}

{{ next_month }}

    {% else %} {% if this_month != next_month %}

{{ next_month }}

    {% endif %} {% endif %} {% endif %} {% endfor %}

    It is only a matter of where do you make the cut on the loop.

提交回复
热议问题