Hi,
I’m trying to filter subpages by authors:
controllers:
if($author = param('author')) {
$author = urldecode(param('author'));
$items = $items->filterBy('authors', $author, ',');
}
$authors = $items->pluck('authors', ',', true);
Template:
<ul class="nav-authors">
<?php foreach($authors as $author): ?>
<?php if($author == urldecode(param('author'))): ?>
<li> <a class="active" href="<?= $page->url() ?>"> <span>x</span><?= html($author) ?></a> </li>
<?php else : ?>
<li><a href="<?= url($page->url(), ['params' => ['author' => urldecode($author)]]) ?>"><?= html($author) ?></a> </li>
<?php endif;?>
<?php endforeach ?>
</ul>
This works: when author link is clicked, page shows articles from correspondant author
Problem: It shows author email, not his name, and if multiple authors are used, the pluck does nor seem to work.
So I tried (from cookbook) to make it this way:
Controllers:
if($author = param('author')) {
$author = urldecode(param('author'));
$items = $items->filterBy('authors', $author, ',');
}
// fetch all used authors emails from all articles
$emails = $page->children()->listed()->pluck('authors', '-', true);
// get a list of all authors filtered by the ones with articles
$authors = $kirby->users()->filterBy('email', 'in', $emails);
and in the template:
<ul class="nav-authors">
<?php foreach($authors as $author): ?>
<?php if($author == urldecode(param('author'))): ?>
<li> <a class="active" href="<?= $page->url() ?>"> <span>x</span><?= html($author->name()) ?></a> </li>
<?php else : ?>
<li><a href="<?= url($page->url(), ['params' => ['author' => urldecode($author)]]) ?>"><?= html($author->name()) ?></a> </li>
<?php endif;?>
<?php endforeach ?>
</ul>
This works: Only the 2 authors are shown. they are not duplicate, and their name are correctly shown.
But when we try to filter their own articles, there is no result ![]()
Thank you for help telling me what I’m doing wrong.