Hi all,
i tried implementing the cookbook example for creating a blog with the possibility to filter by tag. However i get âUndefined variable: articlesâ.
I use â/news/â instead of â/blogâ in my URLs.
What i try to achieve is that the user is able to select multiple tags which than filter the suggested blogposts + the H1 should change to the Tag1 Tag2 Tagx⌠selected by the user.
Help would be greatly appreciated=)
Blog Controller:
<?php
return function ($page, $kirby, $site, $tag, $pages) {
// Meta
$seo = $kirby->controller('seo' , compact('page', 'site', 'kirby'));
// Override Meta Title
$metatitle = $page->seotitle().' | '.$site->title();
$gallery = $page->images()->sortBy('sort');
$data = compact('gallery');
// 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();
$articles = compact('articles');
$tags = compact('tags');
$tag = compact('tag');
$pagination = compact('pagination');
return a::merge($seo, $data);
};
Blog Template:
<section class="articles">
<?php foreach($articles as $article): ?>
<article>
<a href="<?= $article->url() ?>"><?= $article->title()->html() ?></a>
<header>
<?= $article->image()->crop(500, 300) ?>
<h2><?= $article->title()->html() ?></h2>
<p><?= $article->text()->excerpt(300) ?></p>
</header>
</a>
</article>
<?php endforeach ?>
<!-- sidebar with tagcloud -->
<aside>
<h1>Tags</h1>
<ul class="tags">
<?php foreach($tags as $tag): ?>
<li>
<a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
<?= html($tag) ?>
</a>
</li>
<?php endforeach ?>
</ul>
</aside>
<!-- pagination -->
<nav class="pagination">
<?php if($pagination->hasPrevPage()): ?>
<a href="<?= $pagination->prevPageUrl() ?>">previous posts</a>
<?php endif ?>
<?php if($pagination->hasNextPage()): ?>
<a href="<?= $pagination->nextPageUrl() ?>">next posts</a>
<?php endif ?>
</nav>
Config php:
return [
'debug' => true,
'schnti.cookie.link' => 'datenschutzerklaerung',
'panel' =>[
'install' => true
],
'routes' => [
[
'pattern' => 'news/tag/(:any)',
'action' => function ($tag) {
return Page::factory([
'slug' => $tag,
'template' => 'tag',
'model' => 'tag',
'content' => [
'title' => 'Results for ' . ucfirst($tag),
]
]);
}
]
]
];