Keeping tags while navigating to item, or adding to current url

Hello,
I am building a website at the moment where the navigation works via tags that are organized in a select dropdown, every item (in my code $work) has multiple tags assigned. Now when I open an item i still want the select dropdown to show the previously selected tag. Right now this is not the case because I linked with the url of the item but is there a way to just add to the current url with the selected tag and then add /item? I couldn’t find anything (or I didn’t word my search entry right).

so right now this → http://localhost:8080/tag:books
links to this → http://localhost:8080/damask#page://A9tIyhW7eofn5OEk
but it should link to this → http://localhost:8080/tag:books/damask#page://A9tIyhW7eofn5OEk

my code currently looks like this (both parts are in different snippets)

first the code for the select menu

<?php 

$filterBy = get('filter');

    $works = $site
    ->children()
    ->not('error')->not('home');
    if($tag = param('tag')) {
        $works = $works->filterBy('tags', $tag, ',');
      }
    $filter = $site->children()->listed()->pluck('tags', ',', true);
?>
                <div class="label"><div class="title" onclick="location.href='<?= $site->url() ?>';"> Notes on</div></div>
                <select id="menu-desktop" class="menu-desktop" onchange="window.open(this.value,'_self');">
                <option selected="" hidden="">… <?php echo html($tag) ?></option>
                <?php foreach ($filter as $filter): ?>
                    <option value="<?= $site ?>/tag:<?= $filter ?>">
                        … <?= $filter ?>
                    </option>
                <?php endforeach ?>
                <option value="<?= $site ?>/us/tag:us">
                        … us
                    </option>
                    <option value="<?= $site ?>/imprint/tag:imprint">
                        … imprint
                    </option>
                </select> 

and here is the code for the “thumbnails” of the items that then link to their pages, where the issue is with the href

<?php 



    $works = $site
    ->children()
    ->not('error')->not('home');
    if($tag = param('tag')) {
        $works = $works->filterBy('tags', $tag, ',');
      }
      ?>


<div class="works">
            <?php foreach ($works as $work): ?>

                <div class="worksItems" id="<?= $work->Uuid() ?>">
                    <a href="<?= $work->url() ?>#<?= $work->Uuid() ?>">
                        <?php if($image = $work->bild()->toFile()): ?>
                            <img style="width:100%" src="<?= $image->url() ?>" alt="">
                        <?php endif ?>
                        <div class="hoverBox">
                            <div class="workTitle"><?= $work->title() ?></div>
                        </div>
                        </a>
                        
                </div>

            <?php endforeach ?>
        </div>

thank you for your help!

I guess “damask” is the page url? Normally the Kirby “params” come after the page slug, so the expected url would actually be

http://localhost:8080/damask/tag:books#page://A9tIyhW7eofn5OEk

As an analogy you can think of them like query parameters, they come after the page:

http://localhost:8080/damask?tag=books#page://A9tIyhW7eofn5OEk

You can construct urls with parameters by passing them as options to the url function:

// $tag = param('tag')
$work->url(['params' => ['tag' => $tag]])
1 Like

And if you need the fragment, you can also add it to the options:

$work->url(['params' => ['tag' => $tag], 'fragment' => $work->uuid()->toString()])

On a site note, I wouldn’t rely on this printing the site URL via the magic __toString() method, better use $site->url() or simply url(), and then you can use the parameter in the options array again, which will also take care of urlencoding the parameters.

1 Like

thank you both so much, my code works now ツ