加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何在Jekyll的主页上按日期分组帖子?

发布时间:2020-12-16 23:20:57 所属栏目:百科 来源:网络整理
导读:在Jekyll,我希望我的主页列出按日期分组的最新帖子,如下所示: 2013年9月6日 发布1 帖子2 发布3 2013年9月5日 发布1 帖子2 基本上,我只想在循环中的帖子与之前处理的日期不同时吐出日期标题.我试图通过测试for循环中的下一篇文章是否匹配上一篇文章的日期来
在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 |日期:“%m-%d-%Y”我改为简单地说post.date它有效,并且帖子被组合在一起,但仅当帖子具有完全相同的日期和时间(不仅仅是月份的同一天) .这就是为什么我添加更具体的post.date |日期:“%m-%d-%Y”.

有任何想法吗?非常感谢我们的帮助!!

解决方法

通过修改存档解决方案找到答案: http://www.mitsake.net/2012/04/archives-in-jekyll/

这是有效的代码:

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,%Y" }}</h5>
    {% endif %}
    {{ post.content }}
    <hr>

{% endfor %}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读