jekyll-paginate-v2 icon indicating copy to clipboard operation
jekyll-paginate-v2 copied to clipboard

Custom key finder

Open kentosama opened this issue 5 years ago • 1 comments

Hello, I would like to know if it's possible to use something other than categories, tags and locales to filter the collections displayed on a page. For example, on my blog, I have game reviews for different platforms. I would like to filter the content for SEGA Genesis, Master System, Game Gear etc. without going through categories or tags.

I also need to filter this "review" collection with other keys like genre, developer etc.

At the moment I work with categories and tags, but it is not practical to filter the content.

Thanks by advance for your help.

kentosama avatar May 06 '20 11:05 kentosama

Had a similar problem where I wanted to have multi language posts only once on the index page. I created a custom plugin which groups the posts by locale, marks the primary post with the category frontpage and then I filtered the paginator by this category.

Same posts with different languages are grouped by the same ref frontmatter value, if no ref is present in the post, the post ID will be used.

module FrontPagePlugin

    class FrontPageGenerator < Jekyll::Generator
        safe true

        def generate(site)

            byref = site.posts.docs.group_by { |p| p.data['ref'] || p.id }

            byref.each do |key, posts|
                # sort locale in reverse order, so that en wins against de
                posts.sort_by { |post| post.data['locale'] }.reverse.each_with_index do |post, postindex|
                    if postindex <= 0
                        post.data['categories'].push('frontpage')
                    end
                end
            end

        end
    end

end

(After modifying the rb file you have to restart the jekyll dev server)

---
# index.html
layout: localizedindex
title: Latest Posts
permalink: /index
frontpage: true
pagination:
  enabled: true
  category: frontpage
---

perryflynn avatar Dec 03 '23 19:12 perryflynn