Author page to list all articles by one author

I have followed the tutorial here: https://getkirby.com/docs/cookbook/author-system

I would like to have a page for each author listing all the articles written by that author i.e. an author.php template rather than the authors.php template suggested at the bottom of the tutorial:

<?php snippet('header') ?>
<?php $articles = $pages->find('blog')->children() ?>

<h1>Authors</h1>

<?php foreach($page->children() as $author): ?>
<article class="author">

  <h1><?php echo $author->name()->html() ?></h1>

  <figure>
    <img src="<?php echo $author->images()->first()->url() ?>">
  </figure>

  <ul class="articles">
    <?php foreach($articles->filterBy('author', $author->uid()) as $article): ?>
    <li><a href="<?php echo $article->url() ?>"><?php echo $article->title() ?></a></li>
    <?php endforeach ?>
  </ul>

</article>
<?php endforeach ?>

<?php snippet('footer') ?>

Do I somehow grab the slug from the url e.g. http://localhost/authors/brad-kitt and use that for filtering? I thought about doing something similar to the tag system but for author but I was going round in circles!

Can anyone please help?

If you have built your authors as subpages, then there is not need to filter the authors, because the authors will already be available under their URL, all you need is the author.php template that matches the author.txt file.

<?php snippet('header') ?>
<?php $articles = page('blog')->children()->visible() ?>
<article class="author">

  <h1><?php echo $page->name()->html() ?></h1>

  <figure>
    <img src="<?php echo $page->images()->first()->url() ?>">
  </figure>

  <ul class="articles">
    <?php foreach($articles->filterBy('author', $page->uid()) as $article): ?>
    <li><a href="<?php echo $article->url() ?>"><?php echo $article->title() ?></a></li>
    <?php endforeach ?>
  </ul>

</article>
<?php snippet('footer') ?>
1 Like

Of course, this one line says it all. It seems obvious now but when you are tired at the end of a long day simple things can seem impossible. It is nice to start the day with another great reply from @texnixe

Thanks

1 Like

I’m doing something very similar (however in Kirby 3) but are running into some problems. Here’s my code:

<section class="l-grid__col l-grid__col--12">
    <h2>Contributions from <?= $page->title() ?></h2>
    <div class="l-grid">
        <?php
        $latest = $kirby->collection("latest")->flip();
        foreach($latest->filterBy('contributor', $page->uid()) as $contribution): ?>
        <a href="<?= $contribution->url() ?>" class="l-grid__col l-grid__col--4">
            <?php snippet('c-teaser', ['article' => $contribution, 'figure' => true]) ?>
        </a>
        <?php endforeach ?>
    </div>
</section>

Each of my article pages has a contrubutor page filed. This looks like this in my content file:

Contributor:

- contributors/test-namerson

Nothing at all is being created by my foreach loop. I’ve tried $page->uid() $page->id() and $page->uri() with no success despite var_dump showing there should be matches. If I remove the filterBy the articles from the collection is shown as expected. I’m really confused!

Since the value is stored in yaml format, we need a separator here:

$latest->filterBy('contributor', $page->id(), '-')
1 Like

Thanks! Is this separator in the wrong place here? This worked for me:

$latest->filterBy('contributor', '-', $page->id())

That doesn’t look correct…

I agree, but it seems to work for me! Very strange…