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”).
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
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 ?>