In Jekyll, I would like my home page to list the most recent posts grouped by date, like so:
在Jekyll,我希望我的主页列出按日期分组的最新帖子,如下所示:
September 6, 2013
2013年9月6日
- Post 1
- Post 2
- Post 3
September 5, 2013
2013年9月5日
- Post 1
- Post 2
Basically, I just want to spit out a date heading when a post in the loop is from a different date from the one previously processed. I've tried to do this by testing if the next post in the for loop matches the date of the last post, and to display a date header only if it doesn't. This is what my Liquid template looks like:
基本上,我只是想在循环中的帖子与之前处理的日期不同时吐出日期标题。我试图通过测试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 %}
If instead of using post.date | date: "%m-%d-%Y"
I instead say simply post.date
it works, and posts are grouped together, but only if the posts have the exact same date and time (not just the same day of the month). That's why I add the more specific post.date | date: "%m-%d-%Y"
.
如果不使用post.date |日期:“%m-%d-%Y”我改为简单地说post.date它起作用,并且帖子被组合在一起,但仅当帖子具有完全相同的日期和时间(不仅仅是月份的同一天) 。这就是为什么我添加更具体的post.date |日期:“%m-%d-%Y”。
Any ideas? Thanks so much for our help!!
有任何想法吗?非常感谢我们的帮助!!
3 个解决方案
#1
6
Found the answer by modifying the archives solution here: http://www.mitsake.net/2012/04/archives-in-jekyll/
通过修改档案解决方案找到答案:http://www.mitsake.net/2012/04/archives-in-jekyll/
Here is the code that works:
这是有效的代码:
layout: default
title: Home Page
---
{% for post in site.posts %}
{% capture day %}{{ post.date | date: '%m%d%Y' }}{% endcapture %}
{% capture nday %}{{ post.next.date | date: '%m%d%Y' }}{% endcapture %}
{% if day != nday %}
<h5 class="date">{{ post.date | date: "%A, %B %e, %Y" }}</h5>
{% endif %}
{{ post.content }}
<hr>
{% endfor %}
#2
3
Alternative solution:
Directly capture the date in the format that you want to display at the end.
(here: %A, %B %d, %Y
--> Monday, April 30, 2012
)
以您希望在结尾显示的格式直接捕获日期。 (此处:%A,%B%d,%Y - > 2012年4月30日星期一)
Then you don't need to use | date:
that often:
然后你不需要使用|日期:通常:
{% for post in site.posts %}
{% capture currentdate %}{{post.date | date: "%A, %B %d, %Y"}}{% endcapture %}
{% if currentdate != thedate %}
<h2>{{ currentdate }}</h2>
{% capture thedate %}{{currentdate}}{% endcapture %}
{% endif %}
<h3><a href="{{ post.url }}">{{ post.title }}</a></h3>
{% endfor %}
The generated HTML:
生成的HTML:
<h2>Monday, April 30, 2012</h2>
<h3><a href="/2012/04/30/foo/">Foo</a></h3>
<h2>Friday, March 09, 2012</h2>
<h3><a href="/2012/03/09/bar/">Bar</a></h3>
<h3><a href="/2012/03/09/baz/">Baz</a></h3>
#3
1
These previous solutions are fantastic and elegant way to get around the shortcomings of previous versions of Jekyll but luckily in late 2016, Jekyll added a group_by_exp
filter that can do this much more cleanly.
以前的解决方案是解决以前版本的Jekyll的缺点的奇妙和优雅的方法,但幸运的是在2016年底,Jekyll添加了一个group_by_exp过滤器,可以更干净地完成这个。
{% assign postsByDay =
site.posts | group_by_exp:"post", "post.date | date: '%A, %B %e, %Y'" %}
{% for day in postsByDay %}
<h1>{{ day.name }}</h1>
<ul>
{% for post in day.items %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
Documentation can be found on the Jekyll Templates page.
可以在Jekyll模板页面上找到文档。
#1
6
Found the answer by modifying the archives solution here: http://www.mitsake.net/2012/04/archives-in-jekyll/
通过修改档案解决方案找到答案:http://www.mitsake.net/2012/04/archives-in-jekyll/
Here is the code that works:
这是有效的代码:
layout: default
title: Home Page
---
{% for post in site.posts %}
{% capture day %}{{ post.date | date: '%m%d%Y' }}{% endcapture %}
{% capture nday %}{{ post.next.date | date: '%m%d%Y' }}{% endcapture %}
{% if day != nday %}
<h5 class="date">{{ post.date | date: "%A, %B %e, %Y" }}</h5>
{% endif %}
{{ post.content }}
<hr>
{% endfor %}
#2
3
Alternative solution:
Directly capture the date in the format that you want to display at the end.
(here: %A, %B %d, %Y
--> Monday, April 30, 2012
)
以您希望在结尾显示的格式直接捕获日期。 (此处:%A,%B%d,%Y - > 2012年4月30日星期一)
Then you don't need to use | date:
that often:
然后你不需要使用|日期:通常:
{% for post in site.posts %}
{% capture currentdate %}{{post.date | date: "%A, %B %d, %Y"}}{% endcapture %}
{% if currentdate != thedate %}
<h2>{{ currentdate }}</h2>
{% capture thedate %}{{currentdate}}{% endcapture %}
{% endif %}
<h3><a href="{{ post.url }}">{{ post.title }}</a></h3>
{% endfor %}
The generated HTML:
生成的HTML:
<h2>Monday, April 30, 2012</h2>
<h3><a href="/2012/04/30/foo/">Foo</a></h3>
<h2>Friday, March 09, 2012</h2>
<h3><a href="/2012/03/09/bar/">Bar</a></h3>
<h3><a href="/2012/03/09/baz/">Baz</a></h3>
#3
1
These previous solutions are fantastic and elegant way to get around the shortcomings of previous versions of Jekyll but luckily in late 2016, Jekyll added a group_by_exp
filter that can do this much more cleanly.
以前的解决方案是解决以前版本的Jekyll的缺点的奇妙和优雅的方法,但幸运的是在2016年底,Jekyll添加了一个group_by_exp过滤器,可以更干净地完成这个。
{% assign postsByDay =
site.posts | group_by_exp:"post", "post.date | date: '%A, %B %e, %Y'" %}
{% for day in postsByDay %}
<h1>{{ day.name }}</h1>
<ul>
{% for post in day.items %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
{% endfor %}
Documentation can be found on the Jekyll Templates page.
可以在Jekyll模板页面上找到文档。