Hi there,
I’ve followed your amazing youtube course building a portfolio website, and I would like to add a CSS class to an active filter. I’m an absolute beginner, so I don’t really understand how to do that.
In the youtube comments, you said to ‘in the foreach loop for the filter buttons you can compare $filter with $filterBy and then add a css class or an aria-current attribute if they are the same’
but I can’t seem to make it work. I guess it should go somewhere here, but I’m not sure.
Great, thank you! I would like to highlight the “All” Link if no Filter is active. Is there a possibility to check if no filter is selected to add the “active” class to this Link?
Thank you!
Hi, you can use param('filter') to check if a filter parameter is set in the url and if not, you can highlight the ‘All’ link. You can read more e.g. here: Filter collections by tags | Kirby CMS
If you want a custom sorting of the filters that is not alphabetic, then you would have to use something like usort: usort
Hey! I’m also struggling with this. The code snippet here is only applying the filtername as class name to the <a> element - but not a class named “active” or the :active for a class with the filter name?
This is what I’d get for all, no matter which one is active: <a href="http://localhost:8080/projects?filter=Work" class="">Work</a>
and this is what I’d want to achieve:
if not selected: <a href="http://localhost:8080/projects?filter=Work" class="inactive">Work</a>
if selected: <a href="http://localhost:8080/projects?filter=Work" class="active">Work</a>
This line of code doesn’t return a simple array, but an array of field objects. Therefore the strict comparison
$filterBy === $filter
Does not work.
You have two options:
Use
$filterBy == $filter
or, in my opinion the better option, pass a comma as separator in the pluck method even if your field does not contain a comma separated list of value (which makes sure you get a simple indexed array)
Hi there! Thank you for all the helpful replies.
I’ve added an active class to the filters, but I’m wondering how to highlight ‘All’ when no filter is selected? Do I need to use the param() method, and if so, how? I’m still using the same code as in the YouTube tutorial. Thanks in advance!