Kirby Tags Options

Hi there,

I have tags with a value and text. For example: the tag ‘Cohabitation Visa’ should have the id ‘cohabitationvisa’. How do I list all the tags with their text (and not value)?

sidebar:
    width: 1/3
    sections:
      meta:
        type: fields
        fields:
          date:
            type: date
            time: true
            default: now
          author:
            type: users
          tags:
            type: tags
            options:
              - value: cohabitationvisa
                text: Cohabitation Visa
              - value: settlingin
                text: Settling In
              - value: funstuff
                text: Fun Stuff

Two options:

  1. Get the text property from the blueprint, using the $page->blueprint() method, see details how to access a single field and its properties here: https://getkirby.com/docs/cookbook/templating/blueprints-in-frontend

  2. Store the options in an array in your config and access them from there via the options helper.

Thank you Texnixe!

I have a follow up question: how do I make a helper function that I can access anywhere in my blog?

I made this php function:

<?php 
function getMyTag($taggy) {
   $tags = $kirby->option('tags');
  $key = array_search($taggy, array_column($tags, 'id'));
  return $tags[$key]['value']; } 
?>

If I include the helpers.php file in a page, I get the error “Undefined variable: kirby”

I don’t know where you put that function, but you should put it into a plugin file: /plugins/helpers/index.php, that way you can access it from anywhere. The plugin file is automatically loaded, not need to require it anywhere.

Oh, I forgot the $kirby part: outside of templates etc. you can use kirby():

 $tags = kirby()->option('tags');

or just the option helper:

 $tags = option('tags');