Reversing the Order of a Loop in Hexo

05-17-2019

Problem

I need to reverse the order of posts that are displayed on my site based on they’re creation date, i.e. most recent first.

Solution

With Hexo 3 we have the ability to reverse the order of items in a loop using .each based on a specific parameter defined in the front matter section of our site.

In this example we’re using the date parameter of the post to sort all of the posts in reverse order. The syntax looks like this: .sort('date', -1).

Example

1
2
3
<% site.posts.sort('date', -1).each(function(item){ %>
<%- item.title %></a>
<% }); %>