Skip to content
● Critical PHP security fixes are out: update PHP on your Grav sites. What to do →

Listing all values of taxonomies

Solved by hughbris View solution

Started by James O 8 years ago · 2 replies · 1091 views
8 years ago

I'm just trying to list all entries of a passed in taxonomy for a collection of pages. I have a collection of sermon pages and they have an author and book associated:

YAML
title: 'The Title of the Post'
taxonomy:
    category: 'sermon'
    author: 'Person A'
    book: [Matthew,Mark,Luke]

and

YAML
title: 'Another Title of a Post'
taxonomy:
    category: 'sermon'
    author: 'Person B'
    book: [Mark,Luke,John]

What I want to see is the following:

List of Authors:

  • Person A
  • Person B

List of Books

  • Matthew
  • Mark
  • Luke
  • John

How do I output this list? And where can I learn more about the properties of the taxonomy object besides examples that list tags of a given page/item? Thanks.

last edited 12/18/18 by James O
8 years ago Solution

I remember having some trouble finding this one not long ago! It's just not a very intuitive or documented method name. The Twig I used is:

TXT
taxonomy.getTaxonomyItemKeys('category')

so substitute 'category' for 'book' or whatever.

I figured this out by studying the API docs and code but it involved lots of experimenting too, since as I mentioned it's not well covered. I think there's a way to extract slices of individual vocabs from the taxonomy tree object too, from memory.

Oh outputting as a list would be a matter of looping through the resulting array BTW. Not sure if you need help at that level.

👍 1
8 years ago

This is SUPER HELPFUL, thank you!

This method produces a collection that I can now filter and sort. I also was able to do a count of the matched entries like so using findTaxonomy():

TXT
taxonomy.findTaxonomy({'book':[PASSED IN TAG VALUE]})

Thanks again!

Suggested topics

Topic Participants Replies Views Activity
Support · by webmasterodp, 2 weeks ago
4 244 2 weeks ago
Support · by nwhitfield, 2 weeks ago
2 211 2 weeks ago
Support · by stell, 4 weeks ago
5 457 3 weeks ago
Support · by gravinator, 2 months ago
5 636 3 weeks ago
Support · by Gravver, 1 month ago
6 429 4 weeks ago