Tags and subtags in one array?

Hi,

I’d like to work with tags and subtags.

The first page tag would be the main one, and the following ones would be the sub-tags.
Example for an article: first tag-> “News”, second tag → “News subtag”
Example for another article: first tag-> “Events”, second tag → “Events subtag”, third tag → “Events subtag 2”

How can I change my code here to fetch only first tags in the filter menu?
And then under each filter menu <li> fetch all the tags that are coming in second but have the same first tag? Would that be possible?

<?php
    $filterBy = get('filter');
    $unfiltered = $page->children()->sortBy('date', 'desc');
    $chronicles = $unfiltered
        ->when($filterBy, function($filterBy) {
            return $this->filterBy('Tags', $filterBy, ',');
        });


    $filters = $unfiltered->pluck('Tags', ',', true);
    ?>

<?php foreach ($filters as $filter): ?>
<li class="<?= $filter ?>">
<a href="<?= $page->url() ?>?filter=<?= $filter ?>"><?= $filter ?></a><span></span>
</li>
<?php endforeach ?>

Thanks in advance for the help.
Bests

I am not sure I understand the content 100%. Your content file looks like this?

Tags: Events, Events subtag, Events subtag 2

And now you want to get a list from only the first tag of all pages?

Thank you for your answer.

The content file contains all article at the same level.

They all have multiple tags.
Capture d’écran 2025-01-01 à 15.12.46

I want to make a filter menu with the principal tags (first ones) as first hierarchy and the seconds as subfilters.
For example;

  • News
    –– Store news
    –– Membership news
    –– Releases

  • Events
    –– Speeches
    –– Concerts

Is it possible like this, or do I have to do it differently?
Thank you very much

I think for general convenience I would add a custom page model (Page models | Kirby CMS) adding a method that returns only the main tag, e.g.

class EventPage extends \Kirby\Cms\Page
{
	public function topTag(): string
	{
		return $this->tags()->split(',')[0];
	}
}

Which you could then use for your ->pluck() call.

$filters = $unfiltered->pluck('topTag', unique: true);