Embed images from other pages' image field

I don’t quite understand how to access the url of an image from an image field. In my case I have loaded the page into a variable project (this is not the current page) and in the panel of this page I have an image field cover. At the moment I load it like this:

<img src="<?= $project->contentURL() . "/" . $project->cover() ?>">

While this works, this looks a bit hacky and not quite Kirby-like to me. Is there an easier way that I am overlooking?

Thanks!

This should work:

<img src="<?= $project->cover()->toFile()->url() ?>">

But I’d suggest checking if the image (still) exists:

<?php if($cover = $project->cover()->toFile()): ?>
  <img src="<?= $cover->url() ?>">
<?php endif ?>

If $project is not the current page you could do something like:

<?php if($cover = $project->image($project->cover())): ?>
  <img src="<?= $cover->url() ?>">
<?php endif ?>
1 Like

Thanks @thguenther, this was exactly the information I was looking for. Works like a charm.