Filter by tags : when clicking on a tag, it will stay on the article page

Hi there :slight_smile:
I’m new to Kirby, and new to php.
I tried to apply the cookbook about tag filtering : Filtering with tags | Kirby CMS

What I want to do :

  • on the article page, defined tags should show. This is ok.
  • when clicking on a tag, it should show a page with all articles that have that tag. This doesn’t work.

Here is my ‘article’ template :

<aside>
  <p>Étiquettes</p>
  <ul class="tags">
    <?php foreach($tags as $tag): ?>
    <li>
      <a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
        <?= html($tag) ?>
      </a>
    </li>
    <?php endforeach ?>
  </ul>
</aside>

And here is the ‘article’ controller :

<?php

return function($page) {

// fetch pages
$articles = page('blog')->children()->listed()->flip();

// fetch tags
$tags = $articles->pluck('tags', ',', true);
sort($tags);

// add tag filter
if($tag = urldecode(param('tag'))) {
  $articles = $articles->filterBy('tags', $tag, ',');
}

// apply pagination
$articles   = $articles->paginate(12);
$pagination = $articles->pagination();

return compact('articles', 'tags', 'tag', 'pagination');

};

What am I missing ?
Thank you for your help.

It doesn’t make sense that the controller has the same name as the single article template. Where are you showing your list of articles? Should be the blog controller or whatever intended template the parent uses. Then the url here

should also point to the parent page instead of the same page.

Thank you for your very fast answer.
I’m trying to show the article’s tags on the article’s page.
The list of articles is showing on my blog page.
I renamed article.php controller “blog.php”.
And tried this in the article’s template :

<a href="<?= url($page->parent()->url(), ['params' => ['tag' => $tag]]) ?>">
        <?= html($tag) ?>

But I get an error page now when trying to access the article page.

Should be:

        <a href="<?= $page->parent()->url(['params' => ['tag' => $tag]]) ?>"><?= esc($tag) ?></a>

Apart from that, we are using this type of filtering in the Starterkit. Check out the notes controller (which belongs to the notes page) and note template and controller for the note pages.