16

Liquid with Jekyllを使用して、バンドのWebサイト(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'}}または{{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...

ビルド時にのみ実行されます。Web サイトを真に自動的に更新したい場合は、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 にはスケジュールされたビルドがあり、1 日に 1 回プロジェクトをビルドするように指定するだけです。CloudCannon を使用している場合は、ユーザーの[ここ]の回答をお勧めします。

于 2017-11-12T22:03:09.800 に答える