在 Jekyll 中,我希望我的主页列出按日期分组的最新帖子,如下所示:
2013 年 9 月 6 日
- 帖子 1
- 帖子 2
- 帖子 3
2013 年 9 月 5 日
- 帖子 1
- 帖子 2
基本上,当循环中的帖子与先前处理的日期不同时,我只想吐出一个日期标题。我试图通过测试 for 循环中的下一篇文章是否与上一篇文章的日期匹配来做到这一点,并且只有在不匹配时才显示日期标题。这就是我的 Liquid 模板的样子:
---
layout: default
title: Home Page
---
{% assign thedate = '' %}
{% for post in site.posts %}
{% if thedate != post.date | date: "%m-%d-%Y" %}
<h2>{{ post.date | date: "%A, %B %e, %Y" }}</h2>
{% endif %}
{% assign thedate = post.date | date: "%m-%d-%Y" %}
<h3 class="headline"><a href="{{ post.url }}">{{ post.title }}</a></h3>
{{ post.content }}
<hr>
{% endfor %}
如果不是使用post.date | date: "%m-%d-%Y"
我而是简单地post.date
说它有效,并且帖子被分组在一起,但前提是帖子具有完全相同的日期和时间(不仅仅是一个月中的同一天)。这就是为什么我添加更具体的post.date | date: "%m-%d-%Y"
.
有任何想法吗?非常感谢我们的帮助!!