Home > Enterprise >  Eleventy: Create "View all posts in collection" link
Eleventy: Create "View all posts in collection" link

Time:09-29

I'm trying to build a site in Eleventy. The site has blog posts in two collections: A and B. When I'm reading a post in the collection A, I'd like to have a link after the post content that says, "View all A posts". And when I'm reading a post in collection B, I'd like to see a "View all B posts" link.

The blog posts share the same _includes template post.html. In that template, I should be able to create an if statement in nunjucks that goes something like this:

{% if post in collections.A %}<a href="/a">View all A posts</a>
{% elif post in collections.B %}<a href="/b">View all B posts</a>
{% endif %}

That code snippet doesn't work though. Any ideas?

CodePudding user response:

Given that you have blog posts separated by folder, you can create a directory data file that specifies a value for the directory (A, B). Something like so:

{
"blogtype":"a"
}

In your post template where you want to do the link, you can check the blogtype value to do your link.

Does that make sense? To be clear, you can do logic of "if this post is in collection so and so" with a filter, but I think specifying a directory data file would (probably) be simpler.

  • Related