【发布时间】:2020-07-08 05:40:07
【问题描述】:
我正在尝试在 Jekyll 中按特定日期过滤帖子。例如,我想归档今天、过去 30 天以及之前的所有帖子。
但是我遇到了几个问题:
- 如何获取当前日期,从上午 00:01 开始
- 如何获取当前日期并减去 30 天
- 如何使用
where_exp按此日期过滤
目前我尝试做这样的事情,但这会将日期转换为字符串,并且不能在where_exp中使用:
{% capture thirty_days_ago %}{{'now' | date: '%s' | minus: 2592000 }}{% endcapture %}
{% assign last_30_days_posts = site.posts | where_exp:"post", "post.posted_on > thirty_days_ago" %}
Liquid error (line 22): comparison of Time with String failed in index.html
我可以在遍历帖子时做一个简单的检查,但在此之前我更喜欢使用过滤器。
{% capture thirty_days_ago %}{{'now' | date: '%s' | minus: 2592000 }}{% endcapture %}
{% for post in site.posts %}
{% capture post_date %}{{ post.posted_on | date: '%s' | plus: 0 }}{% endcapture %}
{% if job_date > thirty_days_ago %}
{% include components/job.html job=job %}
{% endif %}
{% endfor %}
【问题讨论】:
-
@BradWest 这是我现有的解决方案,但是我想知道在过滤数组时是否可以这样做。