Hello
I’m in the process of building a blog for my site.
I am a bit stuck with displaying items with the same tag on a page. I’ve managed get the tags to show up on an individual article, but when I click on the tag, a page that opens up displays the pagination of the main blog page. How do I set the pagination to show only on the main blog page and is there a way to show the tag title on the tag page?
Here is my code
main blog page tamplate - magazin.php
<h1><?= $page->title()->html() ?></h1>
<section class="container p-0">
<div class="row p-0 gx-5 gy-5">
<!-- articles -->
<?php foreach($articles as $article): ?>
<div class="col-12 col-md-6">
<?php snippet('article', ['article' => $article]) ?>
</div>
<?php endforeach ?>
</section>
<nav class="mt-5">
<?php if ($pagination->hasPrevPage()): ?>
<a href="<?= $pagination->prevPageURL() ?>" class="button btn-small btn-dark-grey rounded">‹</a>
<?php endif ?>
<?php foreach ($pagination->range(10) as $r): ?>
<a<?= $pagination->page() === $r ? ' aria-current="page"' : '' ?> href="<?= $pagination->pageURL($r) ?>" class="button btn-small btn-dark-grey rounded">
<?= $r ?>
</a>
<?php endforeach ?>
<?php if ($pagination->hasNextPage()): ?>
<a href="<?= $pagination->nextPageURL() ?>" class="button btn-small btn-dark-grey rounded">›</a>
<?php endif ?>
</nav>
magazin.php controller
return function($kirby, $site, $pages, $page) {
$shared = $kirby->controller('site' , compact('page', 'pages', 'site', 'kirby'));
// fetch the basic set of pages
$articles = $page->children()->listed()->flip();
// fetch all tags
$tags = $articles->pluck('tags', ',', true);
// add the tag filter
if($tag = param('tag')) {
$articles = $articles->filterBy('tags', $tag, ',');
}
// apply pagination
$articles = $articles->paginate(10);
$pagination = $articles->pagination();
return A::merge($shared , compact('articles', 'tags', 'tag', 'pagination'));
};
?>
article.php template
<h1><?= $page->title()->html() ?></h1>
<time datetime="<?= $page->date()->toDate('j M, Y') ?>" class="color-grey d-block"><?= $page->date()->toDate('j M, Y') ?></time>
<?php
$tags = $page->tags()->split(',');
foreach($tags as $tag): ?>
<a href="<?= $page->parent()->url(['params' => ['tag' => $tag]]) ?>" class="button btn-small btn-dark-grey">#<?= $tag ?></a>
<?php endforeach ?>
<p class="lead mt-3"><?= $page->intro() ?></p>
<?= $page->text()->toBlocks() ?>
Also, with some of the code I found on the forum, my url was showing up as magazin/tag:tag+name however that didn’t work so I had to change to the code above, now it works.
Is it meant to be a colon or a semicolon?
EDIT: I now realise that the pagination is actually breaking up the items under that tag. So it’s actually showing the number of pages for that particular tag.
Remaining questions: how do I get the #tag title to show up on the tag page?