Include user avatar with user name in posts/blog pages

So i finally managed to find time to start digging in and learning how the guts of Kirby actually works. I learn by breaking things, basically. I’ve learned a LOT today! ha!

So somehow I have managed to successfully edit my site info, footer, article blueprint, article template and blog template. I have mostly implemented the IndieWeb microformat h-entry code, minus a few things. One thing I need is to import the user avatar with the username. For this blog, that user is me. No other users, so that should simplify things. If I do a second blog, that one will have two authors, but I’ll cross that bridge when I come to it.

Do I need to add a separate image field to the blueprint before calling it from the template? I did the user name that way, but was wondering if I need to clutter up the panel, especially since, again, there’s just one author here…

Thanks in advance for your assistance!

You can add an avatar to the Panel user, then get it in your template via the $user->avatar()method if that fits your use case, see example in the docs: https://getkirby.com/docs/cheatsheet/user/avatar

1 Like

In that cheatsheet, the $user->avatar() is not actually used in their example code. And their example mentions a specific user, not code to call the user.

So if I have a user with an avatar (added in the panel or yml, either way) and that user creates a post, how do I call the user/author and their avatar? Currently I have an author field in the blueprint and call it using <?= $page->author() ?> but I can’t insert that into the example PHP without breaking the page. Each post will have an author w/ an avatar, just trying to display both in each article (next to the timestamp).

Replace the hardcoded user name with the field value:

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

If this throws an error, use $page->author()->value() instead.

i tried both… they throw an error

this gives me the correct username (demlak)
<?php echo $article->author() ?>
also this:
<?php echo $article->author()->value() ?>

this works:
<?php if($avatar = $site->user('demlak')->avatar()): ?>

this throws an error (Call to a member function avatar() on null)

<?php if($avatar = $site->user($article->author()->value())->avatar()): ?>
same here
<?php if($avatar = $site->user($article->author())->avatar()): ?>

@demlak The code above will work if you use Panel users. Are you talking about Panel users? The code won’t work like this if you use the author system you described in the other post?

Edit: It’s better to use an if-statement anyway to make sure the user exists first (might be that a user is stored in the file and later removed):

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

@texnixe
thx a lot for your help… this in here is not related to the other thread… =)
i did not know, that it is possible to add extra info to panel users… and read out all of them. =)
so i try this instead of extra author-system

i’m able to print the authors name (in my example it is demlak, which is the same as the panel-user) by:

<?php echo $article->author() ?>

if i add the username manualy, then the if-statement works.

<?php if($avatar = $site->user('demlak')->avatar()): ?>

i still get an error on this:

<?php if($avatar = $site->user($article->author())->avatar()): ?>

i can’t get $article->author() to work inside user()

also your latest suggestion throws an Undefined variable: user-error

here is the part i’m talking about

    <?php $articles = $pages->find('blog')->children()->visible()->flip()->paginate(3) ?>

    <?php foreach($articles as $article): // article overview ?>

      <article class="post">
        <header>
          <div class="title">
            <h2><a href="<?php echo $article->url() ?>"><?php echo html($article->title()) ?></a></h2>
            <p><?php if($article->tags() != ''): ?>
            <?php foreach(str::split($article->tags()) as $tag): ?>
            <a href="<?php echo url('tag:' . urlencode($tag)) ?>">#<?php echo $tag; ?></a>&nbsp;
            <?php endforeach ?>
          <?php endif ?></p>
          </div>
          <div class="meta">
            <time class="published" datetime="<?php echo $article->date('c') ?>"><?php echo $article->date('F dS, Y'); ?></time>


            <?php echo $article->author()->avatar() // just for testing ?>

            <?php if($avatar = $site->user('demlak')->avatar()): ?>
            <a href="#" class="author"><span class="name"><?php echo $article->author() ?></span><img src="<?php echo $avatar->url() ?>" alt="avatar"></a>
            <?php endif ?>

          </div>
        </header>
        <?php if($image = $article->images()->sortBy('sort', 'asc')->first()): ?>
        <a href="<?php echo $article->url() ?>" class="image featured"><img src="<?php echo $image->url() ?>" alt="<?php echo $article->title()->html() ?>" /></a>
        <?php endif ?>

        <p><?php echo excerpt($article->text(), 400) ?></p>
        <footer>
          <ul class="actions">
            <li><a href="<?php echo $article->url() ?>" class="button big">Continue Reading</a></li>
          </ul>
          <ul class="stats">
            <li><a href="#">General</a></li>
            <li><a href="#" class="icon fa-heart">28</a></li>
            <li><a href="#" class="icon fa-comment">128</a></li>
          </ul>
        </footer>
      </article>


    <?php endforeach // article overview ends ?>

Ok, we need to add some parentheses:

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

perfect! thx a lot =)

yes - this worked - thank you!