Cliquable custom metatags links for pages

Hi,

I’ve created custom metatags for pages.
I want to create a cliquable tag list to have on the same page the pages who have the clicked tag (like tags pages for blog).

Here is the code in blueprint:

tags_article_authors:
   label: Quoted authors
   type:  tags 
   separator: /

Here is the code in page.txt (please note the / tags separator)

----
Tags-article-authors: 
Name1, Surname1 /
Name2, Surname2

That code in php template display tag names on a list, without cliquables links:

<ul>   
<?php // https://forum.getkirby.com/t/tags-as-separate-items-in-an-html-list/1122 ?>
<?php foreach($page->tags_article_authors()->split('/') as $tag): ?>   
<li><?php echo $tag ?></li>   
<?php endforeach ?> 
</ul>

I can have cliquable tags list with the following code, but the result page (when we click one tag) is blank:

<ul>   
<?php foreach($page->tags_article_authors()->split('/') as $tag): ?>   
<li><?php echo $tag ?></li>                   
<a <?php ecco($tag == $p, ' class="active"') ?> href="<?php echo url('tag:' . $tag)?>">
<?php echo html($tag) ?>
</a>                
<?php endforeach ?> 
</ul>

Do I have to create a php template for tags pages? Is there a model somewhere ?
I read that post: http://getkirby.com/blog/custom-metatags but it didn’t help for having “true” links.

Thanks for helping,
Kind regards,
Anthony

The official tags tutorial in the documentation mentions creating a tag page. Maybe that is a good starting point.

Your tags contains spaces and commas, so you need to urlencode() and urldecode() them again. Also, you need to filter your results by the tags field and also specify the page where the results should be shown.

Thanks @texnixe. Do you have a sample code example? I’m new with Kirby.

It would be easier, if I knew where your tags are and where you want to display the results …

In your controller for the page where you need your results, you need something like this:

...
$tag = urldecode(param('tag'));
if(param('tag')) {
    	$articles = $section->children()->visible()->filterBy('tags', $tag,',')->sortBy('date')->flip();
    } else {
    	$articles = page('blog')->children()->visible()->sortBy('date')->flip();
  }
...

This is code from a blog template. Adjust the page, your variables and the name of your parameters as required.

Then in your link, you need to specify the url of the page, where the results are supposed to appear (in this example, it would be the blog page) and the name of the parameter, if it is not called “tag”. Something like this:

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

In this bit <?php echo url('blog/tag:' . $tag)?> I specify the the page (blog) and the parameter name (tag).

Thank you @texnixe, I think I’m on the good way.

Tags are in subpages from the page who display results.

Here is the code thanks to you to display tags, it works well :

<h2>Authors</h2>
            <ul>   
                <?php foreach($page->tags_article_authors()->split('/') as $tag): ?>   
                <li><a <?php ecco($tag == urldecode(param('tag')), ' class="active"') ?> href="<?php echo url('articles/author:' . $tag)?>"><?php echo $tag ?></a></li>                
                <?php endforeach ?> 
            </ul> 

The following code provide links like this:

http://www.mywebsite.com/articles/author:Random%20guy%201

Note that I can have special cars in tags like space.

So I want to display the results on the “articles” pages, which is parent to pages who contain tags.
Here is the code I tried to display results:

  <?php 
  //
  // Here I want the name of the displayed tag
  // Results for tag "author tag 1"
  //
$tag = urldecode(param('tags_article_authors'));
if(param('tags_article_authors')) {
        $page->visible()->filterBy('tags_article_authors', $tag,'/');
        //
        // Here I want to have a list of cliquable links who contain the displayed tags
        // <li><a href="URL from post 1 who contain tag 'author tag 1'">Title of the post 1</a></li>
        // <li><a href="URL from post 2 who contain tag 'author tag 1'">Title of the post 2</a></li>
        //
    } else {
        // do other stuff
        echo 'no results';
  }
  ?> 

I don’t know where to add my custom name " tags_article_authors()" on that code to display them.
I also don’t know if have to adjust the results code with the url variable (here : “/author:tagname”)

I hope you’ll have all useful informations, let me know if I can help.
Also thank you very much for helping me.

Kind regards,
Anthony

Well, first of all, I just realized I had a typo in my above code, it should of course be urlencode() in the href attribute of your authors list:

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

Secondly, your tag is called “author” in your link tag, so in the controller of your articles.php you should have:

<?php
  return function($site, $pages, $page) {
    $tag = urldecode(param('author'));
    if(param($tag)) {
       $authorPages =  $page->children()->visible()->filterBy('tags_article_authors', $tag,'/');
    } else {
      $authorPages = "";  //usually, you would show all pages by all authors instead ...
     // $authorPages = $page->children()->visible();
    }
  return compact('authorPages');
};

Next, in the articles.php template :smile:

<?php foreach($authorpages as $authorPage) {
  echo $authorPage->title(); //etc.
}
?>

If you have any further question, pls. don’t hesitate to ask.