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

前端 未结 8 1188
無奈伤痛
無奈伤痛 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 %}
        <h2 id="{{ this_year }}-ref">{{this_year}}</h2>
        <ul>
        {% endif %}
    
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
    
        {% if forloop.last %}
        </ul>
        {% else %}
            {% if this_year != next_year %}
            </ul>
            <h2 id="{{ next_year }}-ref">{{next_year}}</h2>
            <ul>
            {% 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 %}
        <h2 id="{{ this_year }}-ref">{{this_year}}</h2>
        <h3 id="{{ this_year }}-{{ this_month }}-ref">{{ this_month }}</h3>
        <ul>
        {% endif %}
    
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
    
        {% if forloop.last %}
        </ul>
        {% else %}
            {% if this_year != next_year %}
            </ul>
            <h2 id="{{ next_year }}-ref">{{next_year}}</h2>
            <h3 id="{{ next_year }}-{{ next_month }}-ref">{{ next_month }}</h3>
            <ul>
            {% else %}    
                {% if this_month != next_month %}
                </ul>
                <h3 id="{{ this_year }}-{{ next_month }}-ref">{{ next_month }}</h3>
                <ul>
                {% endif %}
            {% endif %}
        {% endif %}
    {% endfor %}
    

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

    0 讨论(0)
  • 2020-11-28 05:16

    Try:

    {% for post in site.posts  %}
      {% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
    
      {% if forloop.first %}
      <h2 id="{{ this_year }}-ref">{{this_year}}</h2>
      <ul class="posts">
      {% else %}
          {% if this_year != last_year %}
          </ul>
          <h2 id="{{ this_year }}-ref">{{this_year}}</h2>
          <ul class="posts">
          {% endif %}
      {% endif %}
    
        <li>
          <span class="post-date">{{ post.date | date_to_string }} &raquo;</span>
          <a href="{{ post.url }}">{{ post.title }}</a>
        </li>
    
      {% if forloop.last %}
        </ul>
      {% endif %}
    
      {% capture last_year %}{{ this_year }}{% endcapture %}
    {% endfor %}
    
    0 讨论(0)
提交回复
热议问题