Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Use the taxonomy value as classname for styling

Solved by Hugh Barnes View solution

Started by lesabu 8 years ago · 2 replies · 579 views
8 years ago

Hi, i am using taxonomies to categorise my content. Now I would like to style each category differently. so lets say all posts with 'taxonomy: category: - apple' have a green background. And all 'taxonomy: category: - berry' have a red background.

How can I use the category name as a class for styling?

I tried in de base.thml.twig:
<body class="{{ page.header.taxonomy.category }}">

but that doesnt work 😭

How do I do this?

Thanks for help!

👍 1
8 years ago Solution

Great idea, you just have the Twig expression slightly wrong. I had to check in the docs for the correct expression, so don't feel bad 🙂

page.taxonomy.tag would work, but it's an array because you can have more than one term. If you know there's always one, try page.taxonomy.tag|first. Otherwise you need to compile a delimited string of classnames composed from each term: page.taxonomy.tag|join(' ') should do it.

👍 1
8 years ago

ooh 😱 I think thats it what I mean! Thank you!!

I am new to grav/programming and I think I need to check these theme variables a bit more. they seem quite – cool ;)

👍 1

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 198 2 months ago
Themes & Styling · by Ian, 2 months ago
3 94 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 455 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 48 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 128 3 months ago