Linking tags within articles

Hey folks,

I’m trying to set it up so I can add tags to my blog posts, then in turn have those tags linked to a page that shows all other posts with the same tag.

I’ve kinda got it working - the post contains links with the tags, but when I click on the links, the it just shows all posts.

Here’s the appropriate code from my article.php template. I’m sure I’m doing something stupid wrong. I’m new to Kirby, so apologies if I am.

<ul class="post-meta">
  <?php foreach ($page->tags()->split() as $tag): ?>
    <li><a href="<?= $page->parent()->url(['params' => ['tag' => $tag]]) ?>"><?= esc($tag) ?></a></li>
  <?php endforeach ?>
</ul>

Thanks,

Kev

In the controller or template of your parent page, do you filter by tags? Could you post your code, please?

Hi,

Sorry for the delay in reply, I’ve been out all day today. I don’t filter by tags by the looks of things. Here’s mt blog.php template as it currently stands:

<body>
    <?php snippet('header') ?>

    <?php
        $articles  = $page->children()->listed()->paginate(20)->sortBy('published', 'desc');
        $pagination = $articles->pagination();
    ?>

    <main>
        <h1 class="hidden"><?= $page->title() ?></h1>
        
        <?php foreach($articles as $article): ?>
            <article>
                <h2 class="post-title"><a href="<?= $article->url() ?>"><?= $article->title()->html() ?></a></h2>
                <p class="date"><?= $article->published()->toDate('d M Y') ?></p> 
            </article>
        <?php endforeach ?>
        
        <?php if ($pagination->hasPages()): ?>
        <div class="post-nav">
            <div>
                <?php if ($pagination->hasPrevPage()): ?>
                <a class="pagination-links" href="<?= $pagination->prevPageURL() ?>">← Newer Posts</a>
                <?php endif ?>
            </div>
            <div class="post-nav-next">
                <?php if ($pagination->hasNextPage()): ?>
                <a class="pagination-links" href="<?= $pagination->nextPageURL() ?>">Older Posts →</a>
                <?php endif ?>
            </div>
        </div>
        <?php endif ?>

    </main>

    <?php snippet('footer') ?>
</body>
</html>

After reading the post you linked to, I assume I would have to change my blog.php template to something like this?

<body>
    <?php snippet('header') ?>

    <?php
        $articles  = $page->children()->listed()->paginate(20)->sortBy('published', 'desc');
        $pagination = $articles->pagination();
        if($tag = param('tag')) {
            $articles = $articles->filterBy('tags', $tag, ',');
          }
    ?>

    <main>
        <h1 class="hidden"><?= $page->title() ?></h1>

// REST OF TEMPLATE FILE...

Huzzah! That worked. Thank you so much for the help. These forums are the best.

You might consider moving this logic into a controller (maybe at a later state when you are more familiar with Kirby).

Yeah, I’m still learning the ropes, but once I’m more comfortable, I’ll definitely be making use of things like controllers to clear up my code. Thanks again, you’re the best.