Tag-based pagination?

Let’s assume I have a blog with multiple topics (tags): food, music, politics.

Is it possible to somehow determine the next and previous articles, within a topic, for pagination? Let’s say that someone is interested in only the “music” topic, within the current article, I’d rather have the pagination link to the next article for that topic only (and not stray into the politics topic, for example).

Somehow using current_article.tags and something like next_article and something like blog.articles.index current_article all wrapped together?

This way, each topic could basically be it’s own self-contained blog.

It’s definitely possible. I have something akin to this, but I’m not using the Middleman blog plugin.

The trick is simply to iterate over the sitemap looking for resources with tags metadata. For instance, to return a list of pages containing a given tag, you could write a helper that looks something like:

def articles_by_tag(tag_name)
  sitemap.resources.select do |resource|
    resource.data.tags.include?(tag_name)
  end
end

I haven’t tested this code, but you should get the gist.

1 Like