16

我正在使用带有 Jekyll 的 Liquid 在我的乐队网站 (http://longislandsound.com.au) 上发布日期

我想要的是自动隐藏旧日期,这样我就不必再进去删除它们了。我认为最好的方法是将发布日期与当前日期进行比较,并且仅在日期在未来时才显示帖子,但我不知道该怎么做。

这是当前代码:

<ul id="dates">
{% for post in site.posts reversed %}
<a href="{{post.link}}">
<li>
    <div class="date">
        <span class="day">{{post.date | date: "%d"}}</span><br />
        <span class="month">{{post.date | date: "%b"}}</span>
        <span class="week">{{post.date | date: "%a"}}</span>
    </div>
    <div class="details">
        <span class="venue">{{post.venue}}</span><br />
        <span class="town">{{post.town}}</span>
    </div>
</li>
</a>
{% endfor %}
</ul>

我尝试了一些 if 语句,但我不知道如何将发布日期与当前日期进行比较。

任何人都可以帮忙吗?

4

2 回答 2

35

基于date-math-manipulation-in-liquid-template-filterget-todays-date-in-jekyll-with-liquid-markup,您应该能够使用{{'now'}}or{{site.time}}和难以找到的 unix 时间戳日期的组合筛选| date: '%s'

{% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
{% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
{% if posttime < nowunix %}
...show posts...

捕获的数字可能充当字符串,而不是数字,并且可以使用以下技巧将类型转换回数字:

{% assign nowunix = nowunix | plus: 0 %}
于 2014-04-12T03:44:16.247 回答
5

尽管此代码有效:

{% capture nowunix %}{{'now' | date: '%s'}}{% endcapture %}
{% capture posttime %}{{post.date | date: '%s'}}{% endcapture %}
{% if posttime < nowunix %} ...show posts...

它仅在构建期间执行。如果您希望您的网站真正自动更新,您应该让 javascript 进行隐藏。

从这个液体开始:

{% for item in site.events %} 
  <div future-date="{{ item.date | date: '%Y%m%d' }}">...</div> 
{% endfor %}

并添加这个 javascript:

function getCompareDate() { 
  var d = new Date(), 
      month = '' + (d.getMonth() + 1), 
      day = '' + d.getDate(), 
      year = d.getFullYear(); 
  if (month.length < 2) month = '0' + month; 
  if (day.length < 2) day = '0' + day; 
  return [year, month, day].join(''); 
} 

$('[future-date]').each(function() { 
  if($(this).attr('future-date') < getCompareDate()) $(this).hide(); 
});

解决方案在这里找到:http: //jekyllcodex.org/without-plugin/future-dates/


更新(2018-02-19):
CloudCannon 现在已经安排了构建,您可以简单地指定每天构建一次项目。如果您使用 CloudCannon,我推荐用户[here]的答案。

于 2017-11-12T22:03:09.800 回答