【发布时间】:2018-03-22 04:26:26
【问题描述】:
我不确定能否获得流畅的语法来帮助我提取在构建日期同一周发布的帖子。那可能吗?
有一些更简单的方法可以获取最近的帖子列表,但这种方法对我的项目很有用。我已经尝试了几件通过搜索找到的东西,但还没有任何乐趣。
【问题讨论】:
我不确定能否获得流畅的语法来帮助我提取在构建日期同一周发布的帖子。那可能吗?
有一些更简单的方法可以获取最近的帖子列表,但这种方法对我的项目很有用。我已经尝试了几件通过搜索找到的东西,但还没有任何乐趣。
【问题讨论】:
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 = 10label_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>
【讨论】:
在重新考虑这个答案之前,请考虑一个事实,至少这个答案的第一部分并没有真正起作用,因为 jekyll 是一个 static 站点生成器,因此显示的帖子是相对于上次构建日期的,可能与当前日期不同。
答案的第二部分更深入地探讨了实际生成“最近的帖子”列表的想法,而不是“上周的帖子”。
基本上用文字解释代码:首先我们得到current year和current week,然后我们循环遍历每个帖子并将current year和current week与帖子的week和year进行比较。如果匹配,则显示帖子。
展示 - 构建周:
{% 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 -%}
【讨论】:
build date 和 post date 的代码如果距离您上次发帖已经超过一周,并且您在调整布局或类似之后再次构建网站,则可能不会显示任何内容添加新页面或任何不是新博客文章的内容。从这个意义上说,比较 latest post date 和 post date 的代码更安全。
你永远不知道 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();
});
【讨论】: