How to change the default order pages in Jekyll?

Ever picture Ever · Nov 7, 2012 · Viewed 22k times · Source

My blog is built with Jekyll on Github. In the navigation bar, the default order is Pages, Messages, About, Archives. I want to change the list to Pages, Archives, About, Messages. What should I do?

I think it is related to the code below

{% assign pages_list = site.pages %}

I think site.pages is what I should change, but I don't know how.

Answer

Victor Stegaru picture Victor Stegaru · Nov 29, 2015

You can create custom order of your menu items like this:

  1. In your pages front matter add the order field (you can name it as you prefer)
    ---
    layout: default
    published: true
    title: Page title
    order: 1
    ---
    
  2. When getting pages, apply the 'sort' filter
    {% assign sorted_pages = site.pages | sort:"order" %}
    {% for node in sorted_pages %}
      <li><a href="{{node.url}}">{{node.title}}</a></li>
    {% endfor %}
    

You'll end up with an ordered (ASC) list of pages, based on the 'order' field value you add to each page.