In Jekyll, how to show “posts from last week”

后端 未结 3 1204
暖寄归人
暖寄归人 2021-01-20 05:35

I\'m not sure to get the liquid syntax to help me pull posts that were published during the same week as the build date. Is that possible?

There are simpler ways to

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-20 06:03

    You never know when Jekyll builds, so Jekyll should output a big(ger) list of posts. You can use the code of Joonas for this. Then you should use javascript to hide the non-relevant posts (those older than one week).

    This can easily be done by adding a custom attribute to your list, like this:

  • {{ post.title }}
  • Use jQuery (or vanilla js) to hide old posts:

    // 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

提交回复
热议问题