【问题标题】:In Jekyll, how to show "posts from last week"在 Jekyll 中,如何显示“上周的帖子”
【发布时间】:2018-03-22 04:26:26
【问题描述】:

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

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

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    above solution 有效,但不会跨越数年。

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

    Liquid date/time 使用 unix 时间戳 (%s = seconds since 1970)。所以我在几秒钟内保留了timeframe,并在一段时间内进行了转换。 86400 = 1 day, 604800 = 1 wk, 2678400 = 31 days, ... 等等。

    该代码还假设您的帖子在您的帖子前端使用last_modified_at。如果不是,您可以替换 post.date

    简单的解决方案

    列出上周内的帖子

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

    另类

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

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

    • timeframe 2419200 = 4 周内的秒数
    • maxposts = 10
    • label_FOOBAR 只是用液体处理 html 的干净方式

    带有新的/修改的标志和日期的示例结果

    代码

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

    【讨论】:

      【解决方案2】:

      在重新考虑这个答案之前,请考虑一个事实,至少这个答案的第一部分并没有真正起作用,因为 jekyll 是一个 static 站点生成器,因此显示的帖子是相对于上次构建日期的,可能与当前日期不同。

      答案的第二部分更深入地探讨了实际生成“最近的帖子”列表的想法,而不是“上周的帖子”。

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

      展示 - 构建周:

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

      为了挽救这个答案,虽然它已经偏离了方向......并显示当年和那周发布的所有帖子。

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

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

      显示 - 最新发布周:

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

      显示 - 最新发布日和前 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 -%}
      

      【讨论】:

      • 这需要 Jekyll 每天构建。这不是一个非常现实的场景。
      • 我还是喜欢这个代码。最好在 HTML 中获得大致正确的帖子,或者在您使用 cron 进行日常构建时(使用 Travis CI)。使用 Travis CI 和对此代码的一些小调整,您还可以安排帖子(使用 Jekyll 中的“未来”选项)。
      • 我认为这个答案仍有一些价值。它确实有点工作......取决于你想从这个帖子列表中得到什么以及你如何命名它。 — 如果您上次更新了网站,比如说一年前,这个帖子列表将包含相对于上次更新(构建日期)的“最近帖子”。话又说回来,如果您继续对网站进行风格上的更改并在不发布任何新帖子的情况下构建它,它可能会失败......
      • 为了清楚起见,我已经修改了我的问题。我所说的“今天”或“现在”是指当前的构建日期。本质上,我正在寻找一种在构建日期后 7 天内显示帖子的方法。
      • @DylanKinnett 因为代码并没有完全按照您的要求进行,所以我添加了更多代码,实际上可以按照您的要求进行操作,并且我还添加了图像以更好地说明它们的工作原理。我觉得有必要再说一遍...使用比较 build datepost date 的代码如果距离您上次发帖已经超过一周,并且您在调整布局或类似之后再次构建网站,则可能不会显示任何内容添加新页面或任何不是新博客文章的内容。从这个意义上说,比较 latest post datepost date 的代码更安全。
      【解决方案3】:

      你永远不知道 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();
      });
      

      【讨论】:

      • 虽然这看起来是一个不错的方法,但我想我更喜欢 Joonas 所描述的那种仅限 jekyll 的解决方案。
      • 没有 Jekyll 唯一的方法来解决这个问题。 Jekyll 网站就像预印本杂志。你永远不知道他们什么时候会被阅读。唯一的例外是需要每天重建您的网站的 cron 作业。
      猜你喜欢
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多