在Jekyll中,如何显示“上周的帖子" [英] In Jekyll, how to show "posts from last week"

查看:69
本文介绍了在Jekyll中,如何显示“上周的帖子"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否可以使用简洁的语法来帮助我提取在构建日期的同一周内发布的帖子.那可能吗?

I'm not sure to get the liquid syntax to help me pull posts that were published during the same week as the build date. Is that possible?

有更简单的方法来获取最近的帖子列表,但是这种方法对我的项目很有用.我已经尝试了一些通过搜索找到的东西,但还没有发现喜悦.

There are simpler ways to get a list of recent posts, but this approach would be useful for my project. I've tried several things I found by searching but no joy yet.

推荐答案

上述解决方案可以使用,但不能使用数年.

The above solution works, but doesn't span years.

因此,我利用了这些概念,并提出了一种更简单,更灵活的解决方案来过滤timeframe(可以是几秒钟,几小时,几天,几周或几个月的任意可变时间跨度).我的解决方案具有更少的变量和更少的逻辑.

So I utilized the concepts and came up with a simpler and more flexible solution for filtering for a timeframe (which can be any variable time span of seconds, hours, days, weeks, or months). My solution has fewer variables, and less logic.

液体date/time使用Unix时间戳(%s = seconds since 1970).因此,我将timeframe保留了几秒钟,并进行了一段时间的转换. 86400 = 1 day604800 = 1 wk2678400 = 31 days,...等等.

Liquid date/time uses unix timestamp (%s = seconds since 1970). So I kept the timeframe in seconds and do the conversion for the length of time. 86400 = 1 day, 604800 = 1 wk, 2678400 = 31 days, ... and so on.

代码还假定您的帖子在帖子的前题中使用last_modified_at.如果不是,可以用post.date代替.

The code also assumes your posts use last_modified_at in your post frontmatter. You could substitute for post.date if you're not.

列出上周内的帖子

{% assign timeframe = 604800 %}

{% for post in site.posts %}
  {% assign post_in_seconds = post.last_modified_at | date: "%s" | plus: 0 %}
  {% assign recent_posts = "now" | date: "%s" | minus: timeframe  %}

  {% if post_in_seconds > recent_posts %}
    <a href="{{ post.url }}">{{ post.title }}</a>
  {% endif %}
{% endfor %}


替代


Alternative

列出时间范围内的帖子并标记为新的或已修改

此代码将列出所有帖子,并将其限制在一个范围内,并在timeframe中将其标记为新帖子或已修改.列表的长度限制为maxposts. 注意: css类旨在利用Bootstrap,根据您的喜好删除/编辑.

This code will list all posts to within a limit and flag an posts as new or modified in timeframe. Length of list is limited to maxposts. Note: css class is designed to utilize Bootstrap, remove/edit to your liking.

  • timeframe 2419200 = 4周内的秒数
  • maxposts = 10
  • label_FOOBAR用液体处理html的干净方法
  • timeframe 2419200 = seconds in 4 weeks
  • maxposts = 10
  • label_FOOBAR just clean way to handle html with liquid

带有新标记/已修改标记&的示例结果日期

代码

{% assign timeframe = 2419200 %}
{% assign maxposts = 10 %}
{% assign date_format = site.minima.date_format | default: "%m/%d" %}

<ul class="post-list text-muted list-unstyled">
{% for post in site.posts limit: maxposts %}
  {% assign post_in_seconds = post.last_modified_at | date: "%s" | plus: 0 %}
  {% assign recent_posts = "now" | date: "%s" | minus: timeframe %}
  {% assign post_updated = post.last_modified_at | date: date_format %}
  {% capture post_date %}<small>{{ post.date | date: date_format }}</small>{% endcapture %}

  {% if post_in_seconds > recent_posts %}
  {% capture label_new %}<span class="label label-primary">new</span>{% endcapture %}
    {% if post.last_modified_at > post.date %}
      {% assign label_new = '' %}{% comment %}Clear NEW if modified{% endcomment %}
      {% capture label_updated %}<span class="label label-info">Updated <span class="badge">{{ post_updated }}</span></span>{% endcapture %}
    {% endif %}
  {% endif %}
  <li>
    <h4>{{ post_date }}
      <a class="post-link" href="{{ post.url | relative_url }}">
        {{ post.title | escape }}</a> {{ label_new }}{{ label_updated }}
      </h4>
  </li>
  {% assign post_date = '' %}
  {% assign label_updated = '' %}
{% endfor %}
</ul>

这篇关于在Jekyll中,如何显示“上周的帖子"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆