Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust Jekyll post order?

I have started a Jekyll based blog with a theme jekyll-now. I am new to Jekyll and how it operates (especially Liquid). I understand that new posts need to be as follows: 2014-10-04-Hello-World.md. But I really don't understand how I could order these.

My first thought is that they order by date so two posts on the same date would order randomly. But is this not the case? Is there a way of ordering posts chronologically. OR at least having a post archive page?

like image 526
Koxzi Avatar asked Oct 04 '14 19:10

Koxzi


3 Answers

There is an example in the official Jekyll documentation how to create a basic post archive page:
Displaying an index of posts

Bonus: For a prettier archive page (grouped by year or year/month), see this answer.


You're right, I can't find anything in the docs where it says how the posts are ordered, but in fact Jekyll does order them chronologically, with the most recent post first (you can see this if you try the examples I linked above).

To sort them the other way (the oldest post first), you can use the reversed keyword, according to the Liquid documentation:

{% for post in site.posts reversed %}

However, I don't know how two posts on the same date are ordered, because I don't write that much posts, so I never had that problem :-)
You have to try that yourself.

like image 112
Christian Specht Avatar answered Nov 08 '22 18:11

Christian Specht


Just faced the same problem and solved with this solution: https://groups.google.com/forum/#!topic/jekyll-rb/8QCIzevauSU

Add a date field to the YAML Front Matter of a post, like so:

date: 2010-09-15 14:40:45

e.g. if you have 2 posts on 2014/12/31, you can add date: 2014-12-31 00:30:00 to latest_post.md, and date: 2014-12-31 00:10:00 to older_post.md.

You can add time zone (e.g. date: 2014-12-31 00:10:00 +08:00) if needed

like image 28
Yi-Ping Shih Avatar answered Nov 08 '22 16:11

Yi-Ping Shih


I want to document my struggle into this post so it may help other users. You need to do two changes:

  1. Open your posts and add weight. e.g., weight:100
  2. Open your html file for the menu where you want the sorted posts. For Java/J2EE menu I have java.html file at the root path of my project.

Then, add the {% assign pages_list = pages_list | sort:"weight" %} line as shown in the below code. This will sort by weight.

{% for category in site.categories %} 
  {% if category[0] contains 'java' %} 
    <h3 id="{{ category[0] }}-ref">{{ category[0] | join: "/" }}</h3>
    <ul>
      {% assign pages_list = category[1] %}  
      {% assign pages_list = pages_list | sort:"weight" %}  
      {% include JB/pages_list %}
    </ul>
  {% endif %}
{% endfor %}
like image 20
Ashish Kumar Mondal Avatar answered Nov 08 '22 16:11

Ashish Kumar Mondal