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.

Archive

How to get active taxonomy

Started by Muut Archive 12 years ago · 6 replies · 715 views
12 years ago

Hi,

I'm new with Grav and i'm liking it so far, it is extremely flexible.
I'm trying some things and I'm using the taxonomy list to display the blog categories. I am trying to get the current taxonomy so I could give the <li> an active class. Any ideas how best to do this?

Thanks in advance.

12 years ago

You can get the current page's taxonomy with page.taxonomy() this returns a nested array of taxonomy types (try doing {{ dump (page.taxonomy()) } in your Twig template with debugger on)

It will contain all taxonomy types so if you want to see the current category it would be something like:

TWIG
{% set page_category = page.taxonomy().category|first %}

This will get the first category (you probably only have one), then you can compare that to the list you are outputting and if they are equal, add the active class.

12 years ago

Brilliant!

{{ page.taxonomy().category|first }} works just fine, however I am unable to use {{ dump (page.taxonomy()) }}
in the same page even though debugger is on, why is that, it returns nothing?

Now this works on blog items which has taxonomy set, how would I get the selected taxonomy on the blog item list where the url is like category:news. On this page there is no taxonomy set so I can't use the page.taxonomy(). What do you suggest?

Thanks in advance.

12 years ago

Oh, the debug bar wasn't loading :)

Still trying to figure out how to get the current taxonomy from the querystring

12 years ago

The solution was pretty straight forward:

{% set activeTax = uri.param('category') %}

PS: Sorry for spamming the forum haha

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1318 9 years ago
Archive · by Muut Archive, 9 years ago
2 915 9 years ago
Archive · by Muut Archive, 9 years ago
2 4024 9 years ago
Archive · by Muut Archive, 9 years ago
1 2900 9 years ago
Archive · by Muut Archive, 9 years ago
3 1082 9 years ago