Sorting Jekyll posts by modification date instead of posted date?

梦想的初衷 提交于 2019-12-06 04:16:37

EDIT: It only works in DEVELOPMENT, not sure why it doesn't work in production as well. It updates ALL posts with the same date whenever I deploy the website.

I'm very thankful for hooks and this post ofc: https://stackoverflow.com/a/36769049

Steps:

  • created new file in _plugins folder named hook-add-last-modified-date.rb
  • paste this code inside it and save:

    Jekyll::Hooks.register :posts, :pre_render do |post|
    
    # get the current post last modified time
    modification_time = File.mtime( post.path )
    
    # inject modification_time in post's datas.
    post.data['last-modified-date'] = modification_time
    
    end
    
  • when I hit CTRL+S on a post now 2 things happen: a) updates the last modified date wherever I have {{ post.last-modified-date | date_to_xmlschema }} and b) it bumps it to the top of my posts on the index page because it is sorting by that variable.

Love you guys!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!