Issues with fetching single image

I searched the solution for my issue and found this topic:

Somehow it’s not working and I cann’t understand why. This is my template:

title: About us
icon: ☎️
status:
  draft: true
  listed: true
options:
  changeSlug: false
  delete: false
...
      coverImage:
        type: files
        headline: Cover Image
        max: 1
        template: cover
      files:
        type: files
        headline: Files

My template should show only the url and it looks like:

 <?php if($coverImage = $page->files()->findBy('template', 'cover')) {
  echo $coverImage->url();
} ?>

But there is absolutely no content shown on the page for that line.

My .txt file in content folder looks like:

Title: Über mich
----
Coverimage:
- profile.jpg
----

What have I done :smiley:

Since you are showing that something is stored in the content file, coverImage is obviously a field, not a section (although you have added a headline, which is a section property, while fields have labels.

To fetch the content from this field, your code has to look like this

 <?php if ( $coverImage = $page->coverImage()->toFile() ) {
  echo $coverImage->url();
} ?>

Although your code should also return something if you have a file with the template cover attached in the folder… But since you do not assign an upload blueprint, this might not be the case.

1 Like

That worked, thanks!