Skip to content

'Recent posts' section (and directory/filename oopsie) #3057

Closed Answered by pdehaan
robocone asked this question in Q&A
Discussion options

You must be logged in to vote

As for top n posts, I'd probably use a custom filter to slice an array:

        <ul>
            {% for post in collections['blog-posts'] | reverse | head(2) %}
                <li>
                    <a href="{{ post.url }}">{{ post.data.title }}</a>
                </li>
            {% endfor %}
        </ul>
  eleventyConfig.addFilter("head", function (arr = [], count = 5) {
    if (count < 1) return arr.slice(count);
    return arr.slice(0, count);
  });

Or you can see a slightly better version in the eleventy-base-blog at https://github.com/11ty/eleventy-base-blog/blob/d78b03ae1d059a8e69941e49fa0214595bcf828d/eleventy.config.js#L51-L61

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@pdehaan
Comment options

@pdehaan
Comment options

Answer selected by robocone
@robocone
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants