Cover image not found

Kirby version: 3.3.4

Hi, I’m having an issue displaying a cover image. I had it working once but have obviously changed something and I can’t see whats wrong with the set up.

Any pointers appreciated, here’s the blueprints and template code:

/site/blueprints/pages/home.yml

# sidebar
sidebar:
	width: 1/3
	sections:
		sidebar_content:
			type: fields    
			fields:
				cover:
					extends: sections/image
					template: cover
					min: 1
					image:
						ratio: 1/1

/site/blueprints/sections/image.yml

label: Cover Image
type: files
headline: Cover Image
max: 1
layout: cards
info: "{{ file.dimensions }}"

/site/templates/home.php

<?php if ($cover = $page->images()->findBy("template", "cover")): ?>
  <?= $cover ?>
  <?php else: ?>
  <p>Cover image not found!</p>
<?php endif ?>

And the image is really there?

Yes :slight_smile:

I was thinking I had a sticky cache, but flushed Chrome and tried Safari but no joy. I’m on MAMP.

And does it really have the cover template?

As far as I understand I don’t need a custom ‘cover.yml’ or template.

I added the following code to test for the image file and I was able to print the cover image url:

if ($cover = $page->cover()->toFile()) {
    echo $cover->url();
}

You don’t need a template, that’s true, but before you were searching for the cover via a cover template: $cover = $page->images()->findBy("template", "cover").

You’re not doing that anymore, that’s why your new code works.
You could rework your initial code to the following:

<?php if ($cover = $page->cover()->toFile()) : ?>
    <img src="<?= $cover->url() ?>" ?>
<?php else : ?>
    <p>Cover image not found!</p>
<?php endif ?>

It’s a different approach though. Before you were looking for the cover in all of the page’s images via a template, now you are checking a file field.

1 Like

Excellent, thanks that works and I would say targeting the file field is a more focused approach. But I guess each approach has its merits and depends on how you want to use the panel.

If you have a field cover like you do in your blueprint, it makes sense to fetch the selected image from that field.

However, it is still strange that your first code snippet didn’t fetch the image because it should have (unless the image didn’t have the template assigned for some reason).

Yes, I agree… particularly as I had it working at one point. I might create a new installation to test it out to try to understand what changed.