This demo uses Eleventy to:
- create a collection using custom
categories
andthemes
taxonomies - create a collection of all unique
categories
andthemes
values used throughout ablogposts
collection - create a new 2D array of items based on Zach's method
That 2D array of chunked collection items can then be paginated using pagination
with a size of 1
to create paginated lists of items in each category and theme.
The demo code uses a blogposts
collection as an example.
- run
npm install
- run
npm build
to build pages - run
npm dev
for local development
./eleventy/libs/getCollectionKeyValues.js
is responsible for returning an array of all unique key values used in the front-matter of the target collection. In this demo, "categories" and "themes" are used as keys for which to find unique values.
collection
: the actual collection of items you want to extract an array of unique values from. This collection can be defined as usual, usingtags
or other methods likegetFilteredByGlob()
.key
- the key for which we need unique values. In this case we pass "categories" and "themes" as keys.
Returns an array of objects. Each of the returned objects contains a title
, a slug
and an itemsNumber
keys.
title
: valueslug
: slugified value (used for URLs)itemsNumber
: number of items in the collection that include the value for the passed key
./eleventy/libs/getCollectionByKey.js
is responsible for transforming the passed collection into a 2D array of chunked collection items. That 2D array of chunked collection items can then be used to create paginated "categories" and "themes" pages.
collection
: the target collection from which the new 2D array of chunked items will be created. That new array can be paginated using an Eleventypagination
with a size of1
.key
: the key used to create the new 2D array of chunked items based upon the passed collection.itemsPerPage
: the number of items per paginated pages. Defaults to 10.
Returns an array of objects
title
: unique value for the passed keyslug
: slugcurrentPage
: current page in the paginationtotalItems
: total number of paginated itemstotalPages
: total number of pagesitems
: items on the current pagehrefs
: all pagination hrefs in order,href
first
: first href,last
: last href,next
: next href,previous
: previous href,