简体   繁体   中英

How to show modified posts first in Jekyll?

I know that, Jekyll will show the list of posts depending on the day given in the file like that

2015-01-29-name-of-post.md

On some days later, for example, on 2015-05-12, I have an update on this post and I want Jekyll show it first on the list of "new" posts. I know I can "force" Jekyll to do that by changing the name like

2015-05-12-name-of-post.md

This is not a good idea whereas I want it does automatically. Anyone has other answers?

Here ( http://jekyllrb.com/docs/frontmatter/ ) says there is a special front matter vairable for posts that seem to do what you want:

" date A date here overrides the date from the name of the post. This can be used to ensure correct sorting of posts. A date is specified in the format YYYY-MM-DD HH:MM:SS +/-TTTT ; hours, minutes, seconds, and timezone offset are optional."

So looks like you could add date to your post's front matter and change it that way.

Jekyll's 'last modified at' plugin/Gem does exactly what you want: https://github.com/gjtorikian/jekyll-last-modified-at

It determines the last time a post (or page) file was modified on the disk, and adds a last_modified_at feature to the elements without changing their post dates or urls. Apart from displaying the post.last_modified_at field wherever you like it, you should then also be able to list the posts in the order of their last modification:

<ul>
{% assign posts = site.posts | sort: 'last_modified_at' %}
{% for post in posts %}
  <li>{{ post.title }} (original post date: {{ post.date }})</li>
{% endfor %}
</ul>

Note that the plugin does not work on github pages, so you'll have to build your pages locally. (Due to the way github pages are built, even if they whitelisted the Gem it would show the time of the last push for all files rather than their last edit date.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM