eleventy
eleventy copied to clipboard
Is there a way to find a tag in an indivudal collection instead of all collections?
I have 2 collections...one called post and one called profiles.
collection.posts collection.profiles
I want to be able to find the tag featured in each one not both. Is there a way to do that?
would I have to create a custom collection like "featuredpost" or "featuredprofiles"??
// Collections
eleventyConfig.addCollection("posts", function (collection) {
return collection.getFilteredByGlob("src/_posts/*.md").sort(function(a, b) {
return b.date - a.date;
});
});
eleventyConfig.addCollection("profiles", function (collection) {
return collection.getFilteredByGlob("src/_profiles/*.md").sort(function(a, b) {
return b.date - a.date;
});
});
How would that be done???
Here is the site: https://staging.areyoumodelmaterial.com/
As you can see here they are mixed https://staging.areyoumodelmaterial.com/news/category/featured/
Not certain I understand your issue – if you want the top section to, say, only have posts but not profiles, you could do this by adding a tag either to the front matter for each post or by adding a posts.json file inside the folder with "tags" : "posts".
Then in your template file or the file for this landing page use {% for post in collection.posts %} add html for each post here {% endfor %}. By default posts sort by date.
Does that help?
Can't you use the getFilteredByTag function: https://www.11ty.dev/docs/collections/#getfilteredbytag(-tagname-)
I will try that @peterwgnd. @shanerobinson I don't think that will work. I will try and make a better explanation thanks.
You can filter arbitrarily however you’d like using the Collections API.
See: https://www.11ty.dev/docs/collections-api/#example-get-all-filter
eleventyConfig.addCollection("posts", function (collection) {
return collection.getFilteredByGlob("src/_posts/*.md").filter(item =>
return item.data.tags.includes("included") && !item.data.tags.includes("excluded");
});
});