Print coverImage instead of URL in content filtered by tags

I’m creating a blog-homepage template whereby blog posts are filtered by tags, and shown in the style of a showcase. My problem is that when trying to print the article’s cover-image in the showcase, I’m getting the cover-image’s name instead of the actual image. I’m certain theres something very silly I’m missing, but I just cannot seem to figure it out! My code is as follows:

<?php foreach($site->find('blog')->children()->flip()->limit(100) as $article): ?>
  <div class="blog-item">
      <a  href="<?php echo $article->url() ?>">
        <? if($image = $article->coverimage()): ?>
        <?= $image->resize(500, 400)->html() ?>
        <? endif ?>
        <h1 class="blog-title"><?php echo $article->title()->html() ?></h1>
      </a>
  </div>
<?php endforeach ?>

Any help would be greatly appreciated! Thank you.

coverimage() is the field in your file where Kirby stores the file name. To get the actual file use the toFile() method:

$image = $article->coverimage()->toFile()

Amazing!! Thank you so much.

And don’t forget to check if the image exists :wink:

<?php foreach($site->find('blog')->children()->flip()->limit(100) as $article): ?>
  <div class="blog-item">
      <a  href="<?php echo $article->url() ?>">
        <?php
            $image = $article->coverimage()->toFile();
            if($image): ?>
             <?= $image->resize(500, 400)->html() ?>
          <?php endif ?>
        <h1 class="blog-title"><?php echo $article->title()->html() ?></h1>
      </a>
  </div>
<?php endforeach ?>

Once again, thank you so much!