Sort by newest date? #1822
-
Hello, I'm trying to create a collection of the latest posts (limited to 4) on the homepage of my 11ty site. Everything is currently working as expected, apart from the posts being limited from the oldest onwards. Here's my code:
Is there a way to easily achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think you might be seeing harttle/liquidjs#312 (I think you’re using liquids templates). Possibly something like: ---
posts: [1,2,3,4,5,6,7,8]
---
{%- assign _posts = posts | head: -4 -%}
{% for post in _posts reversed %}
{{ post }}
{% endfor %}
{% comment %}
8
7
6
5
{% endcomment %} // .eleventy.js
module.exports = eleventyConfig => {
eleventyConfig.addFilter("head", (arr, count) => {
if (count < 1) {
return arr.slice(count);
}
return arr.slice(0, count);
});
return {
// ...
};
}; |
Beta Was this translation helpful? Give feedback.
-
I managed to get it working with the following liquid code:
|
Beta Was this translation helpful? Give feedback.
I managed to get it working with the following liquid code: