Closed cjbrooks12 closed 6 years ago
Is there a way to show at static pages the terms for all categories in which the page has been included, just like the Posts plugin does with categories and tags?
I've tried to include these terms in the header.peb template using the {{ term.link }} and {{ term.title }} variables used in the taxonomyArchive templates, but I haven't found a way to fill these variables with the terms the page belongs to for each taxonomy (and in fact, the loop "{% for term in taxonomy.allTerms %}" appears to be empty in the final pages).
It's a bit messy (there's a lot I need to clean up with Taxonomies), but it is possible. Ultimately, there's nothing magical with the Posts plugin's handling of categories and tags, the Taxonomies plugin handles it the same way as adding data in a static page's Front Matter.
You can access the landing pages for each Term using the find()
function in any template, using the term's key. For example, if you have categories foo
and bar
, you can get the landing page for each one like so:
{% set fooCategoryTerm = find(itemId="foo", collectionType="taxonomy") %}
{% set barCategoryTerm = find(itemId="bar", collectionType="taxonomy") %}
From there, you have access to a few properties to explore the other pages in the taxonomy and for that specific term:
fooCategoryTerm.taxonomy
gives you the Taxonomy modelThe Taxonomy Model has some useful properties:
taxonomyModel.landingPage
gives you the landing page for the whole Taxonomy; in this case, the landing page lists all CategoriestaxonomyModel.allTerms
gives you the models for each Term in that Taxonomy as a list of Term modelsFor each Term Model, you also have a few other useful properties:
termModel.landingPage
gives you the landing page for the Term; in this case, the landing page lists all other pages (like Posts and Static Pages) in each specific CategorytermModel.allPages
gives you the Page object for each page in that Term. find()
the landing page for a whole Taxonomy (like all Categories). You'll need to find()
a term within the taxonomy, and then call .taxonomy
to get its parent TaxonomyI don't really need to find all the taxonomies that a page has been included in, I already know which taxonomies are available. What I need is show in the final content page which terms have been applied to that page for each taxonomy.
For example if page xyzzy.adoc has been included in term "foo" of category "bar" (either in its front matter or an archetype), I want to show somewhere in the page the text "bar: foo". But I don't understand how to access from within a page to the name of the terms that include it.
Try the something like following snippet, it should work from any template:
{% set taxonomyModel = find(itemId=page.categories|first, collectionType="taxonomy").taxonomy %}
{% for termModel in taxonomyModel.allTerms -%}
- [{{ taxonomyModel.landingPage.title }}]({{ taxonomyModel.landingPage.link }}): [{{ termModel.landingPage.title }}]({{ termModel.landingPage.link }})
{% endfor %}
For a page with Front Matter (or archetype) like
title: 'Example Page'
categories:
- 'category1'
- 'category2'
You'll get the following output:
This plugin should search the entire Index and group pages into custom taxonomies, in a manner similar to Hugo. Content should not be restricted to any single generator, and is read generically from Front Matter, so can work with any Page type, and also takes Archetypes into account, making it easy to apply taxonomies to a wide range of pages without needing to maintain it on each.
This would effectively replace blog Tags (but perhaps still keep Categories, as they will eventually be hierarchical and are managed by directory), and would eliminate the need for Post Series as a special thing in the Posts plugin. I may want to remove generated Post archives, in lieu of Taxonomy pages that target the Categories.
Internally, Taxonomies are a late-running Generator, which reads all currently-indexed pages for taxonomies, building paginated archives for them all, as well as landing pages for the taxonomies showing the different taxonomy type and the terms under it. Taxonomies can each define permalinks for generated pages, and also decide whether a page can have multiple terms for a single taxonomy. Taxonomies also generate Collections of related pages.
Plugin Features
Taxonomy Configuration Options