Tags and filtering

hey,

i have a question regarding tags in kirby:

the child pages of a page have a structured field including tags. on the frontend i want to parent page to output all the childs with the corresponding tags as class and the tags as select field.

right now i have the following set up which causes some problems:

  • tags aren’t plucked to a single instance. if two children have the same tag, the tag is displayed twice.
  • the tags aren’t “slugged” so ists hard to filter for them.

blueprint:

stations:
label: Audiostation
type: structure
style: table
fields:
  station_text:
    label: Text
    type: textarea
  station_tags:
    label: Tags
    type: tags
    index: all

php for filtering

<div class="filter">
<h2><?php echo l::get('topics') ?>:</h2>
<select class="filter__selector">
  <option value="all"><?php echo l::get('choose') ?></option>
  <?php foreach($page->children()->visible()->filterBy('translationComplete', '1') as $child): ?>
    <?php $stations = $child->stations()->toStructure();
    $categories = $stations->pluck('station_tags', ',', true);
    foreach($categories as $category):
      $filteredProjects = $stations->filterBy('station_tags', $category, ',');?>
      <option value="<?= $category ?>"><?= $category ?></option>
    <?php endforeach ?>
  <?php endforeach ?>
</select>

thanks for any help!

That’s not really surprising, because you are plucking the tags on a per page basis. The following iterations in the loop do not know anything of the previous ones.

I think you should separate both loops, i.e. pluck all tags per loop and then merge everything in a single array, using array_unique().

<div class="filter">
  <h2><?php echo l::get('topics') ?>:</h2>
  <select class="filter__selector">
    <option value="all"><?php echo l::get('choose') ?></option>
    <?php foreach($page->children()->visible() as $child): ?>
      <?php
        $stations = $child->stations()->toStructure();
        $categories = $stations->pluck('station_tags', ',', true);

        foreach($categories as $category):
          $allCat[] = $category;
        endforeach;
      ?>
    <?php endforeach ?>

      <?php foreach(array_unique($allCat) as $category):
          $filteredProjects = $stations->filterBy('station_tags', $category, ',');?>
          <option value="<?= $category ?>"><?= $category ?></option>
        <?php endforeach ?>
  </select>
</div>

edit2: nevermind. i screwed up. somehow some of the german post were marked as english posts. sorrrry. and thanks!