Lesson 10—Categories and Pagination

In this section, we will learn:

Code example of tagList collection

eleventyConfig.addCollection("tagList", function(collection) {
  let tagSet = new Set();
  collection.getAll().forEach(function(item) {
    if( "tags" in item.data ) {
      let tags = item.data.tags;

      tags = tags.filter(function(item) {
        switch(item) {
          // this list should match the `filter` list in tags.njk
          case "all":
          case "nav":
          case "post":
          case "posts":
            return false;
        }

        return true;
      });

      for (const tag of tags) {
        tagSet.add(tag);
      }
    }
  });

  // returning an array in addCollection works in Eleventy 0.5.3
  return [...tagSet];
});

HTML to list all tags

---
pagination:
  data: collections
  size: 1
  alias: tag
  filter:
    - all
    - nav
    - post
    - posts
    - tagList
  addAllPagesToCollections: true
permalink: /category//
eleventyComputed:
  title: ""
---

# 

Posts in <a href='/category/'></a> category. List [all categories](/categories/) or [all posts](/archives).

<ul class="post-list">

</ul>

Comments