在 Jekyll 中,如何显示 "posts from last week"

In Jekyll, how to show "posts from last week"

我不确定是否有流畅的语法来帮助我提取在构建日期同一周内发布的帖子。那可能吗?

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

你永远不知道 Jekyll 何时构建,所以 Jekyll 应该输出一个大的(ger)帖子列表。您可以为此使用 Joonas 的代码。那么你应该使用 javascript 来隐藏不相关的帖子(那些超过一周的帖子)。

这可以通过向您的列表添加自定义属性轻松完成,如下所示:

<li date="{{ post.date }}">{{ post.title }}</li>

使用 jQuery(或 vanilla js)隐藏旧帖子:

// loop through all list items with a date
$('li[date]').each(function(){
  // create a postDate in a date object
  var postDate = new Date($(this).attr('date'));
  // create an object with the date of one week ago
  var oneWeekAgo = new Date();
  oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
  // compare dates and hide old posts
  if(postDate<oneWeekAgo) $(this).hide();
});

Before even giving this answer a second thought, consider the fact that, at the very least the first part of this answer doesn't really work, because jekyll is a static site generator and therefore the shown posts are relative to the last build date, which may not be the same as current date.

The second part of the answer goes a bit deeper into the idea of actually generating a list of "recent posts", rather than "posts from last week".

基本上用文字解释代码:首先我们得到current yearcurrent week,然后我们遍历每个post并比较current yearcurrent week 到 post 的 weekyear。如果它们匹配,则显示 post。

显示 — 构建周:

{% assign currentYear = site.time | date: "%Y" %}
{% assign currentWeek = site.time | date: "%W" %}

{%- for post in site.posts -%}
    
    {% assign postYear = post.date | date: "%Y" %}
    {% assign postWeek = post.date | date: "%W" %}
    
    {%- if currentYear == postYear and currentWeek == postWeek -%}
        <a href="{{ post.url }}">{{ post.title }}</a>
    {%- endif -%}
    
{%- endfor -%}

显示 — 构建日和 6 天前:

{% assign currentYear = site.time | date: "%Y" %}
{% assign currentDay = site.time | date: "%j" | plus: 0 %}
{% assign currentDay_minus_week = site.time | date: "%j" | minus: 7  %}

{%- for post in site.posts -%}
    
    {% assign postYear = post.date | date: "%Y" %}
    {% assign postDay = post.date | date: "%j" | plus: 0 %}
    
    {%- if currentYear == postYear and postDay > currentDay_minus_week and postDay <= currentDay  -%}
        <a href="{{ post.url }}">{{ post.title }}</a>
    {%- endif -%}
    
{%- endfor -%}

为了挽救这个答案,虽然它已经偏离了轨道....我用类似的逻辑编写了这段代码,但这次它得到了 yearweek 的最新 post 并显示所有 posts posted 那一年那一周。

即使您在不创建新的 post 的情况下继续构建站点,这在显示某些内容方面也是无懈可击的。但它也只显示一个 post,如果你的最后一个 post 是那一周唯一的 post post,这可能有点愚蠢...

另一方面,显示“最近的 posts” 的最简单方法可能只是使用 limit 并将最近的 posts 限制为喜欢最后 5 [=62] =]s 或类似的东西:{%- for post in site.posts limit: 5 -%}

显示 — 最近 Post 周:

{% assign latestPost_year = site.posts.first.date | date: "%Y"  %}
{% assign latestPost_week = site.posts.first.date | date: "%W"  %}

{%- for post in site.posts -%}
    
    {% assign postYear = post.date | date: "%Y" %}
    {% assign postWeek = post.date | date: "%W" %}
    
    {%- if latestPost_year == postYear and latestPost_week == postWeek -%}
        <a href="{{ post.url }}">{{ post.title }}</a>
    {%- endif -%}
    
{%- endfor -%}

显示 — 最新 Post 天和 6 天前

{% assign latestPost_year = site.posts.first.date | date: "%Y"  %}
{% assign latestPost_day = site.posts.first.date | date: "%j" | plus: 0  %}
{% assign latestPost_day_minus_week = site.posts.first.date | date: "%j" | minus: 7  %}

{%- for post in site.posts -%}
    
    {% assign postYear = post.date | date: "%Y" %}
    {% assign postDay = post.date | date: "%j" | plus: 0 %}
    
    {%- if latestPost_year == postYear and postDay > latestPost_day_minus_week and postDay <= latestPost_day  -%}
        <a href="{{ post.url }}">{{ post.title }}</a>
    {%- endif -%}
    
{%- endfor -%}

The works, but doesn't span years.

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.

Liquid date/time 使用 unix 时间戳 (%s = seconds since 1970)。所以我将 timeframe 保持在几秒钟内,并根据时间长度进行转换。 86400 = 1 day604800 = 1 wk2678400 = 31 days、...等等。

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

简单的解决方案

列出 post 上周内

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

备选方案

在时间范围内列出 posts 并标记新的或修改的

此代码将在限制范围内列出所有 post,并将 post 标记为新的或已在 timeframe 中修改。列表长度限制为 maxposts注意: css class 旨在根据您的喜好利用 Bootstrap、remove/edit。

  • timeframe 2419200 = 4 周内的秒数
  • maxposts = 10
  • label_FOOBAR 用液体
  • 处理 html 的简单方法

带有 new/modified 标志和日期的示例结果

代码

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