# Filter Menu with active classes

**URL:** <https://forum.getkirby.com/t/filter-menu-with-active-classes/25429>\
**Category:** Questions\
**Tags:** v3\
**Created:** [May 6, 2022, 2:34pm UTC](https://forum.getkirby.com/t/filter-menu-with-active-classes/25429 "2022-05-06T14:34:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aengustukel](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@aengustukel](https://forum.getkirby.com/u/aengustukel)\
**Post date:** [May 6, 2022, 2:34pm UTC](https://forum.getkirby.com/t/filter-menu-with-active-classes/25429/1 "2022-05-06T14:34:32Z")

</div>

I’m trying to make a news list with a filter menu and active classes.  
I’ve followed the tutorial [here](https://www.youtube.com/watch?v=ue15rQVQCqg). I can change the URL the buttons and show the tags in the posts but I can not get the filter to work or incorporate the active class.

Code:

```auto
  <?php 
    $filterBy = get('filter');
    
    $posts = $page
      ->children()
      ->listed()
      ->when($filterBy, function($filterBy){
        return $this->filterBy('category', $filterBy);
      })
  ?>

  <main>
    <a href="<?= $site->url() ?>" aria-label="click to close" class="button-close md button"><?= $site->closeText() ?></a>
    <section class="main-content">

      <h1 class="xxxl h1 ta-center"><?= $page->title() ?></h1>

      <ul class="filter">
        <li><a href="<?= $page->url() ?>" arial-label="show All news" class="md <?= param('category') === null ? 'class="active"' : '' ?>">All</a></li>
        <li><a href="<?= $page->url() ?>?filter=Updates" arial-label="filter the list showing just news with Updates tags" class="md <?= param('category') === true ? 'class="active"' : '' ?>">Updates</a></li>
        <li><a href="<?= $page->url() ?>?filter=Process" arial-label="filter the list showing just news with Process tags" class="md <?= param('category') === true ? 'class="active"' : '' ?>">Process</a></li>
        <li><a href="<?= $page->url() ?>?filter=Press" arial-label="filter the list showing just news with Press tags" class="md <?= param('category') === true ? 'class="active"' : '' ?>">Press</a></li>
        <li><a href="<?= $page->url() ?>?filter=AsGaeilge" arial-label="filter the list showing just news with As Gaeilge tags" class="md <?= param('category') === true ? 'class="active"' : '' ?>">As Gaeilge</a></li>
      </ul>

      <?php
      $posts = $page->children()->listed()->sortBy(function ($page) {
        return $page->published()->toDate();
      }, 'desc');

      foreach($posts as $post): ?>
      <div class="card-news">
        <a href="<?= $post->url() ?>" arial-label="Click to read more about <?= $post->title()->html() ?>">
          <?php if($image = $post->cover()->toFile()): ?>
          <figure>
            <img src="<?= $image->url() ?>" alt="<?= $image->alt() ?>">
          </figure>
          <?php endif ?>
           <time class="sm date-time"><?= $post->published()->toDate('jS F Y') ?></time>
          <h2 class="lg"><?= $post->title()->html() ?></h2>
          <p><?= $post->postContent()->toBlocks()->excerpt(250) ?></p>
          <?php foreach ($post->category() as $category): ?>
            <span class="tag sm"><?= $category ?></span>
          <?php endforeach ?>
        </a>
      </div>
      <?php endforeach ?>

    </section>
  </main>

```

and my blueprint:

```auto
right:
        width: 1/3
        sections:
          postSection:
            type: fields
            fields:
              cover:
                label: Cover
                type: files
                layout: cards
                template: image
                image:
                  ratio: 4/3
                  cover: true
                max: 1
              category:
                label: Category
                type: select
                required: true
                options:
                  - Updates
                  - Process
                  - Press
                  - As Gaeilge
                help: Tag the post as Updates, Process, Press or As Gaeilge
                max: 1
                translate: false
              published:
                label: Published on
                type: date
                display: DD MMMM YYYY
                default: today
                required: true

```

---

<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:** [May 6, 2022, 2:49pm UTC](https://forum.getkirby.com/t/filter-menu-with-active-classes/25429/2 "2022-05-06T14:49:32Z")

</div>

> [@aengustukel](#):
>
> ` <li><a href="<?= $page->url() ?>" arial-label="show All news" class="md <?= param('category') === null ? 'class="active"' : '' ?>">All</a></li>`

To check the active filter, you are querying the category parameter, whereas your URL is using a query.

Also, Your `AsGaeilge` filter cannot work because the value saved in the content file has a space.

So much for these issues, but the main reason the filter doesn’t work is because you are redefining your `$posts` variable after the filter list.

---

<div class="post-metadata">

**Author:** ![aengustukel](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@aengustukel](https://forum.getkirby.com/u/aengustukel)\
**Post date:** [May 6, 2022, 3:29pm UTC](https://forum.getkirby.com/t/filter-menu-with-active-classes/25429/3 "2022-05-06T15:29:28Z")

</div>

How would I solve space in the tag, I’ve tried:

`href="<?= url($page->url(), ['params' => ['tag' => urlencode($tag)]]) ?>"`

I’ve gotten everything else working, the filtering, active classes etc.

---

<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:** [May 6, 2022, 3:57pm UTC](https://forum.getkirby.com/t/filter-menu-with-active-classes/25429/4 "2022-05-06T15:57:30Z")

</div>

If you `urlencode()` on one side, you `urldecode()` on the other side, i.e. where you are reading the param

```auto
urldecode(param('tag'))

```
