Jekyll 按年度显示 archives 文章列表
2008-04-01
大连
/others/2008/04/01/jekyll-archives-year-group.html
others
本文最近更新于 2017 年 10 月 23 日
让 Jekyll 的 archives 按照年度显示文章列表,代码如下:
{% raw %}
<ul class="page_posts">
{% for post in site.posts %} <!-- display the post one by one by for loop -->
{% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %} <!-- get the current post date and make it as this year -->
{% capture next_year %}{{ post.previous.date | date: "%Y" }}{% endcapture %} <!-- get the previous of current post date and make it as next year -->
{% if forloop.first %} <!-- if the current post is the first one -->
<center><p><h1>{{this_year}}</h1></p></center> <!-- display this year -->
{% endif %}
<li>{{ post.date | date:"%Y-%m-%d" }} <span class='postName'>» <a href="{{ post.url }}">{{ post.title }}</a></span></li> <!-- display the post url -->
{% if forloop.last %} <!-- if the current post is the last one, display nothing -->
{% else %}
{% if this_year != next_year %} <!-- if the years are different between these two posts -->
<center><p><h1>{{next_year}}</h1></p></center>
{% endif %}
{% endif %}
{% endfor %}
</ul>
{% endraw %}
其中的 ul center li 部分代码为我所使用的样式。
也可以按照年月显示文章列表,请参考 stackoverflow 的讨论。[20140515添加更新]
关于作者
最近更新: