How to link my article author to my author page

I’m trying to link my author in my article template to the author bio template. I’ve tried using the cookbook method of $pages->find() but end up only getting an error on line 11 that I cannot call a url() on a boolean and had no luck with any other methods I’ve tried.

article.php

    <div class="article-details">
    
    <?php $author = $pages->find('authors/' . $page->author()) ?>
    
    <h1><a href="<?php echo $author->url() ?>"><?php echo $author->name() ?></h1>
    
    <p class="article-date">
      <?= $page->date('F jS, Y') ?>
      <br/>
    </p>
    </div>

The code is correct, but you should always check (with an if statement) if the object you expect (in this case the page object) exists before you call an object’s method like url() or whatever.

<div class="article-details">
    <?php if($author = $pages->find('authors/' . $page->author())): ?>  
      <h1><a href="<?php echo $author->url() ?>"><?php echo $author->name() ?></h1>
    <?php endif ?>
    <p class="article-date">
      <?= $page->date('F jS, Y') ?>
      <br/>
    </p>
</div>

I figured this out though would you be able to help me with another issue of a similar problem?

I changed around my code so that article blueprint pulls the author dirname value instead of the title value for the drop down.

article.yml

  author:
    label: Author
    type: select
    options: query
    query:
      page: authors
      fetch: children
      text: '{{title}}'

This makes it so that the author title can be changed without breaking the find() code in other places since the dirname will probably change less than the author title name like so:

Article.php

<div class="article-details">
         <?php $author = $pages->find('authors/' . $page->author()->lower()) ?>

        <h1><a href="<?php echo $author->url() ?>" ><?php echo $author->title()->html() ?></a></h1>

The problem is that now on my archive main page, the author name shows the dirname instead of the title name in the foreach loop but I can’t figure out how to have it find the page object and then call the title()->html() of it. This code works on other pages. Does something special have to be done in a foreach loop for this to work?

   <?php if($articles->count()): ?>
        <?php foreach($articles as $article): ?>
          <article class="arch-article index" >

            <header class="article-header">
              <h2 class="article-title">
                <a href="<?= $article->url() ?>"><?= $article->title()->html() ?></a>
              </h2>

              <h3 class="article-subtitle"> <?= $article->subtitle()->html() ?></h3>
              <div class="article-details">

                <?php $author = $pages->find('authors/' . $article->author()) ?>

                <h4 class="article-author"> <?= $author->title() ?> —</h4>
                <p class="article-date"∂><?= $article->date('F jS, Y') ?></p>
              </div>
            </header>

          </article>
        <?php endforeach ?>

Thanks for your help!

That looks ok to me. Keep in mind that you better check if the author page exists. Kirby falls back to the UID if there is no title field or if the title is empty. So please check if that is the case.

I’m able to link from subpages of the archive to the author pages . Like so and there is a title field which is filled:

Article.php

    <?php $author = $pages->find('authors/' . $page->author()) ?>

    <h1><a href="<?php echo $author->url() ?>" ><?php echo $author->title()->html() ?></a></h1> 

But when I use a similar code in the Archive.php, I just get the title() boolean error.

How else would I be able to check?

What do you get if you echo oder dump $article->author()? Maybe one of those pages contains a value that does not exist anymore. That’s why I keep preaching to check if the expected object exists. I’m not saying that for nothing.

BTW. In your construct, if the author field is empty and you try to echo the author’s page title, you will get the parent title instead of an author. I think it would be better to either use this syntax:

$author = page('authors')->children()->find($page->author()->value());

Or to store the diruri of the author page.

Ah alright, I’m sorry. I now realize what you meant by it. I managed to get it working now. My issue was I switched the value recently and didn’t fill in all of the author values in my articles. So it caused the Kirby debugger to go off the further it went into the loop.

That’s what you originally meant by checking with an if statement if the object exists. Would I check for that object with $page->exists()?

There is no $page->exists() method. It wouldn’t make sense, either. Because if there was such an object method, it would cause the same sort of error as the title or url methods if the object doesn’t exist :wink:.

All you have to do is this:

if($author = page('authors')->children()->find($page->author()->value())) {
  // do something
}

Or if you define the author variable in advance like above, just

if($author) {
  // do something
}