# Adding classes to active filters

**URL:** <https://forum.getkirby.com/t/adding-classes-to-active-filters/16728>\
**Category:** Questions\
**Tags:** v3\
**Created:** [December 23, 2019, 8:10pm UTC](https://forum.getkirby.com/t/adding-classes-to-active-filters/16728 "2019-12-23T20:10:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vandelay](https://avatars.discourse-cdn.com/v4/letter/v/278dde/32.png) [@vandelay](https://forum.getkirby.com/u/vandelay)\
**Post date:** [December 23, 2019, 8:10pm UTC](https://forum.getkirby.com/t/adding-classes-to-active-filters/16728/1 "2019-12-23T20:10:36Z")

</div>

Hi,  
I’m having some difficulty with something pretty simple in my opinion, but I’m probably missing something quite basic. Following the recipe [Filter collections by tags](https://getkirby.com/docs/cookbook/content/filtering-with-tags), I created a menu to filter a collection of projects. I am trying to add an “active” class to the menu item when selected, but I guess isActive() and isOpen() are not the right way. Would appreciate any help. Thanks

**Controller:**

```auto
return function($page) {

  // fetch the basic set of pages
  $projects = $page->children()->listed();

  // fetch all categories
  $category = $projects->pluck('category', ',', true);

  // add the category filter
  if($category = param('category')) {
    $projects = $projects->filterBy('category', $category, ',');
  }

  return compact('projects', 'category');

};

```

**And then:**

```
$categories = $site->find('works')->children()->listed()->pluck('category', ',', true);
sort($categories);
?>
<ul>
  <?php foreach($categories as $category): ?>
  <li //Add the active class here>
    <a href="<?= url('works', ['params' => ['category' => $category]]) ?>">
      <?= html($category) ?>
    </a>
  </li>
  <?php endforeach ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 23, 2019, 8:20pm UTC](https://forum.getkirby.com/t/adding-classes-to-active-filters/16728/2 "2019-12-23T20:20:29Z")

</div>

This should work:

```auto
  <li class="<?= param('category') == $category ? 'active' : '' ?>">
    <a href="<?= url('works', ['params' => ['category' => $category]]) ?>">
      <?= html($category) ?>
    </a>
  </li>

```

---

<div class="post-metadata">

**Author:** ![vandelay](https://avatars.discourse-cdn.com/v4/letter/v/278dde/32.png) [@vandelay](https://forum.getkirby.com/u/vandelay)\
**Post date:** [December 23, 2019, 8:22pm UTC](https://forum.getkirby.com/t/adding-classes-to-active-filters/16728/3 "2019-12-23T20:22:57Z")

</div>

Worked like a charm 😉 Thanks for the super fast reply!

---

<div class="post-metadata">

**Author:** ![u13712](https://avatars.discourse-cdn.com/v4/letter/u/dfb087/32.png) [@u13712](https://forum.getkirby.com/u/u13712)\
**Post date:** [December 1, 2020, 2:10pm UTC](https://forum.getkirby.com/t/adding-classes-to-active-filters/16728/4 "2020-12-01T14:10:11Z")

</div>

I also want to add a css class to an active filter. But my filter works a little bit different, because I built it like in this video:

[![](https://img.youtube.com/vi/ue15rQVQCqg/maxresdefault.jpg "How to create a filter menu") ](https://www.youtube.com/watch?v=ue15rQVQCqg)

So my url is like this:

`projekte?filter=Theater`

How can I tell Kirby if the part after the `=` in the url is the same as the filter menu option, it should print something?

My controller:

```
return function($site) {

    $filterBy = get('filter');

    $projects = $site
    ->find('Projekte')
    ->children()
    ->listed()
    ->when($filterBy, function($filterBy) { 
        return $this->filterBy('category', $filterBy); 
    })
    ->shuffle();

    return [
        'filterBy' => $filterBy,
        'projects' => $projects
    ];
};

```

My template:

```
<div class="cat-list">
    <a class="cat-button" href="<?= $page->url() ?>">Alle</a>
    <?php foreach ($site->find('Projekte')->category()->split() as $category): ?>
        <a class="cat-button" href="<?= $page->url() ?>?filter=<?= $category ?>"><?= $category ?></a>
    <?php endforeach ?>
</div>

```

The css class `"active"` should be added to the `<a>`

Thank you.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [December 1, 2020, 3:21pm UTC](https://forum.getkirby.com/t/adding-classes-to-active-filters/16728/5 "2020-12-01T15:21:53Z")

</div>

Like this, using get() instead of param()

```auto
 <a class="cat-button<?= get('filter') == $category ? ' active' : '' ?>" href="<?= $page->url() ?>?filter=<?= $category ?>"><?= $category ?></a>
    <?php endforeach ?>
</div>

```

---

<div class="post-metadata">

**Author:** ![u13712](https://avatars.discourse-cdn.com/v4/letter/u/dfb087/32.png) [@u13712](https://forum.getkirby.com/u/u13712)\
**Post date:** [December 1, 2020, 3:56pm UTC](https://forum.getkirby.com/t/adding-classes-to-active-filters/16728/6 "2020-12-01T15:56:53Z")

</div>

Ah nice! Kirby is so easy when you know how it works 😅
