Display Image Field / No Images recognized

Hey, I just started out and am I am facing a noob issue now.

I actually have the same issue as posted previously here in the forum but the solutions don`t work: Getting a URL from new image field

I have a custom image in my blueprint I want to display in my theme (simple onepage):

title: About

fields:
  title:
    label: Title
    type:  text

  coverimage:
    label: Cover Image
    type: image

I simply want to output “coverimage”, but when testing the following code, it displays “0”, wondering why no picture has been found in my folder?

<?php
echo $page->hasImages(); //returns the number of images in the folder
if($page->hasImages()) {
  $images = $page->images();
}
?>

Any help would be really appreciated. All other solutions in found in the forum also ended up in errors (mostly “Call to a member function url() on null”).

Thanks a lot!

Well, you want to get the image you have selected in the coverimage field.

<?php
// check if the image exists (although select in the field, the image might have been deleted)
if($image = $page->coverimage()->toFile()): ?>
<img src="<?= $image->url() ?>">
<?php endif ?>

Thanks, but the strange thing is that this code or any other I found in the similar thread doesn’t output anything on my site, even a picture is selected. It keeps blank :weary:

Maybe the problem is related to using $page here. You said, you are using a one pager. Then $page probably related to the home page and you are using a different variable for your one-pager section? If you followed the one-pager tutorial, you’d have to use the $data variable within the sections to fetch the section data.

Your code would then look like this:

<?php
// check if the image exists (although select in the field, the image might have been deleted)
if($image = $data->coverimage()->toFile()): ?>
<img src="<?= $image->url() ?>">
<?php endif ?>

But without any context, I can’t tell for sure.

Hey, you saved my day! That was the issue! Thank you so much :slight_smile: