In my front matter for some pages (not all) I have:
---
top-navigation:
order: 2
---
Using liquid I want to filter all site pages which have a top-navigation
object and sort by top-navigation.order
.
I'm trying sort:'top-navigation.order'
but that's throwing an exception of undefined method [] for nil:NilClass
. I tried where:"top-navigation", true
but it's not equating truthy values.
How can I filter for pages that have top-navigation
and then sort?
Two steps:
Create an array with pages that contains the top-navigation
key.
We create an empty array and then push the items that have the key.
{% assign navposts = ''|split:''%}
{% for post in site.posts %}
{% if post.top-navigation %}
{% assign navposts = navposts|push:post%}
{% endif %}
{% endfor %}
Sort the above array by top-navigation.order
{% assign navposts = navposts|sort: "top-navigation.order"%}
Printing results:
{% for post in navposts %}
<br>{{ post.title }} - {{post.top-navigation}}
{% endfor %}
For pages use site.pages
.