0
votes

I referred the link on How to customize the sorting of items on a page with jekyll. While the idea is very similar to what I need, the setup doesn't quite work for me.

Scenario:

I have a list of posts, each having only one category, such as category A, category B, category C, and so on. The order I need them in is category B, category A, category C.

Current setup

The current MWE of loop.html is as follows:

{% for category in site.categories %}
  <h3>{{ category[0] | capitalize }}</h3>
  {% for post in category[1] %}
    <h4><a href="{{ post.url }}">{{ post.title }}</a></h4>
    {{ post.excerpt }}
  {% endfor %}
{% endfor %}

This just sorts based on the post date, I believe. I could do the alphabetical sort, but could not get it to be B, A, C.

All of my posts have category, and each has only one category.

When I took the steps shown in the post at the aforementioned link, the loop came off blank.

1

1 Answers

3
votes

Use the same code for each array of categories in the order you want:

{% for category in site.categories.categoryB %}
....

{% for category in site.categories.categoryA %}
...
{% for category in site.categories.categoryC %}
....

You can improve it putting the for loop in an include, an then passing each array of categories as its parameters so you avoid repeating the code.

Another way is to create the array in the order you want and then makebthe for loop.

{% assign result = site.categories.categoryB %}
{% assign result = result | concat: site.categories.categoryA %}
{% assign result = result | concat: site.categories.categoryC %}
 {% for category in result %}
    ....