Avatar of the author/user

I’m following this Topic to include the user (author) avatar and name with:

<?php if(($user = $site->user($page->author())) && ($avatar = $user->avatar())): ?>
<img src="<?php echo $avatar->url() ?>" alt="">
<?php endif ?>

But, I get an empty image:

<img src="(unknown)" alt="">

I can obtain the correct avatar with:

<?php if($avatar = $kirby->user('name@domain.com')->avatar()): ?>
<img src="<?= $avatar->url() ?>" alt="">
<?php endif ?>

But I want the avatar of the author, user, who writes the blog article. Each author has a different avatar.

Thanks in advance.

Assuming we are on the article page and the field name is author:

<?php if ($user = $page->author()->toUser()) : ?>
  <?php if($avatar = $user->avatar()): ?>
  <img src="<?= $avatar->url() ?>" alt="">
  <?php endif ?>
<?php endif ?>
1 Like

And if I’m on the Blog page showing the latest articles?

I don’t have the field name author.

Where is the author stored, then? Not in a field? If in a field but the field is name different, then you will of course have to replace author with the name of your field.

On the blog page, the code would be the same, only you wouldn’t use $page but the variable you use for the single post in the loop.

1 Like

Yes, you’re right, it works now.

Thanks Sonja :wink: