【问题标题】:How to group posts by date on home page in Jekyll?如何在 Jekyll 的主页上按日期对帖子进行分组?
【发布时间】:2013-09-11 05:23:57
【问题描述】:

在 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"。

有什么想法吗?非常感谢我们的帮助!!

【问题讨论】:

    标签: ruby jekyll liquid


    【解决方案1】:

    通过在此处修改存档解决方案找到答案: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, %B %e, %Y" }}</h5>
        {% endif %}
        {{ post.content }}
        <hr>
    
    {% endfor %}
    

    【讨论】:

      【解决方案2】:

      这些以前的解决方案是克服以前版本 Jekyll 的缺点的绝妙而优雅的方法,但幸运的是在 2016 年末,Jekyll added a 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 %}
      

      可以在Jekyll Templates page 上找到文档。

      【讨论】:

        【解决方案3】:

        替代解决方案:

        直接以您希望在末尾显示的格式捕获日期。
        (此处为:%A, %B %d, %Y --> Monday, April 30, 2012)

        那么你就不需要那么频繁地使用| date:了:

        {% 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 %}
        

        生成的 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>
        

        【讨论】:

          猜你喜欢
          • 2012-04-14
          • 1970-01-01
          • 1970-01-01
          • 2021-07-27
          • 2018-06-23
          • 2020-12-08
          • 2019-01-15
          • 2022-10-03
          • 2014-12-06
          相关资源
          最近更新 更多