I’m a bit lost following tutorials and other post about filters, and maybe someone can give me some pointers.
At home.php
, I’m listing my posts grouped by tags/categories in sections. So each section contains posts using that tag. This, I managed to do, researching other examples:
<?php $groups = page('list-design')->children()->listed()->filterBy('category', '!=', '') ?>
<?php
$categories = $groups->groupBy('category');
foreach($categories as $category => $items): ?>
<div class="single-list">
<h2><?= $category ?></h2>
<ul>
<?php foreach($items as $item) : ?>
<li><?= $item->title() ?><div class="icon"></div></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach ?>
What I would like to do, is that each group (and its children) “dissapears” when a filter is applied.
I had something like this from a previous project in the controller
folder, but was however just used for ungrouped posts:
<?php
return function($page) {
$filterBy = get('filter');
$unfiltered = page('list-design')->children()->sortBy('date', 'desc');
$posts = $unfiltered
->when($filterBy, function($filterBy) {
return $this->filterBy('category', $filterBy);
});
$filters = $unfiltered->pluck('category', ',', true);
return [
'filterBy' => $filterBy,
'unfiltered' => $unfiltered,
'posts' => $posts,
'filters' => $filters
];
};
I’ve been struggling to adapt what is in controller to filter a whole tag group, and not individual posts. Even so when right now it doesn’t appear to do anything at all, despite my attempts at trying different possibilities and researching on the filter compedium documentation.
Has anyone had to do anything like this before?
The controls tha apply the filter are:
<a href="<?= $page->url()?>" class="<?= $posts === $unfiltered ? 'active' : '' ?>"><div class="icon"></div>All</a>
<?php foreach ($filters as $filter): ?>
<a href="<?= $page->url() ?>?filter=<?= $filter ?>" class="<?= $filterBy === $filter ? 'active' : '' ?>"><div class="icon"></div><?= $filter ?></a>
<?php endforeach ?>