I would like to automatically insert a last updated timestamp (Not the date
variable for the page) for each post at Jekyll build time, how to achieve that? I think I have to declare a variable but I am not sure how to assign the value to that variable.
For example, some time I update an old post, beside showing the post date, I also want to show the last update date.
I have tried {{Time.now}}
but seems does not work.
The only collection that has a modified_time
is site.static_files
. Not so useful in our case.
One way to get the last-modified-date
for posts in your Jekyll site is to use a hook (documentation).
_plugins/hook-add-last-modified-date.rb
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
It's now available in your posts as : {{ page.last-modified-date }}
.
And you can format this date with the a date filter like {{ page.last-modified-date | date: '%B %d, %Y' }}
. See the Alan W. Smith excellent article on date Jekill Liquid date formating topic.
Important notice : hooks are not working on Github pages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With