List all pages with a given tag

Hello,

I try to build a blog and want to output all pages with a given tag. What I did is:

  1. created a tags page

  2. created a tags controller

     <?php
     // filtert alle Seiten mit param tags
     return function($site, $pages, $page) {
    
       // fetch the basic set of pages
       $articles = $site->index()->filterBy('template', 'default');
    
       // add the tag filter
       if($tag = param('tag')) {
         $articles = $articles->filterBy('tags', $tag, ',');
       }
    
       // fetch all tags
       $tags = $articles->pluck('tags', ',', false);
    
       // apply pagination
       $articles   = $articles->paginate(10);
       $pagination = $articles->pagination();
    
       return compact('articles', 'tags', 'tag', 'pagination'); } ?>
    
  3. created a tags template

  4. in blog template I put this:

     <?php foreach($tags as $tag): ?>
     <li><a <?php ecco($tag == urlencode(param('tag')), ' class="active"') ?> href="<?php echo url('tags/tag:' . $tag)?>">
     <?php echo $tag ?></a></li><?php endforeach ?>
    

If I go to my tags Page, I get all pages, that are in the index

But If I open a page like: tags/tag:Something

I get an error page.

What I am doing wrong?

Thanks!

Hm, I can’t see anything wrong with your code, unless you are using a wrong URL? Is your page really called tags?

yes! I renamed the page to “stichworte”. But it still doesn’t work - I get a 404 error page :frowning: Somehow the part “tag:Something” creates the 404 error. Any ideas?

A parameter shouldn’t actually result in an error page, usually you can add it to any page and it shouldn’t do any harm, even if you are not filtering anything. Is the site accessible?

1 Like

ok, I found now the source of the problem. Somehow my local server mamp affects the url routing. Is mamp a good idea as a local server?

I’m using Mamp myself and I don’t have any problems with it. Probably more of a settings problem? Kirby usually works fine with Mamp.

1 Like

I uploaded the same installation to my web server and had no errors anymore…however, with wamp and xampp it’s the same like mamp, so i guess it’s windows. i just don’t have a clue what it could be :frowning:

Yeah, Windows sounds like a good scapegoat :sunglasses:

1 Like