Global Tags in Footer

Hi - I’m pretty new to Kirby. Coming from CouchCMS was really easy.

I need some help with a small bit of the Tag code. I am trying to have a tag list in the footer of my site that allows someone to go to topics in my journal. I’ve been using this:

<?php foreach($tags as $tag): ?>
    <li>
      <a class="text-dark text-capitalize"  href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
    </a>
  </li>
<?php endforeach ?>

The problem is that it works when on /journal/, but breaks every other page.

How can I tweak this to work across the site without breaking templates?

If I get this right, then you want to go to the journal page when someone clicks on a link, but using $page will always refer to the current page.

Use

<?= url(page('journal')->url(), ['params' => ['tag' => $tag]]) ?>

Thanks for the help with that! It does work:

2020-03-30 at 9.28 AM

Yet if I click to read an article (or any other page) I get this:

This page is currently offline due to an unexpected error. We are very sorry for the inconvenience and will fix it as soon as possible.

Please enable debugging in your /site/config/config.php to get a useful error message.

Thanks! Here is what debug is showing me:

Hm, the error message itself is missing, but it looks like $tags is not defined?

Does this help? Whoops\Exception\ErrorException thrown with message “Undefined variable: tags”

That’s what I assumed. So where do you actually define this variable?

In blueprints > pages > article

I meant where do you define $tags, the variable, ie.e

$tags = ....

ah… Still so new to this!

Site > Controllers > journal.php

And this is my code:

<?php
return function($page) {

$articles = $page->children()->listed()->flip();


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

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

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

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

};

And that screenshot above where the error is thrown, in which file does that happen?

The error occurs on any template that isn’t “journal.php”.

Happens everywhere in the box:
2020-03-30 at 11.13 AM

That is not surprising then :wink:

I suggest you put the tags loop into a snippet (e.g. /site/snippets/tags.php) and inside that snippet define your $tags variable. Then include that snippet in every template where you need it.

Otherwise, your $tags variable is only known in the journal.php template.

Snippet tags.php

<?php
$tags = page('journal')->children()->listed()->pluck('tags', ',', true);

foreach ($tags as $tag): ?>
<!-- Rest of code -->
<?php endforeach ?>

In templates replace the loop with

snippet('tags');

Another option would be to define the tags as a custom collection and then call that collection in the snippet