Page preview image

I’d like to show a custom page preview image (a.k.a. page thumbnail) from a field called “post_image”, and if not specified, the first image (as is default). I’ve got the first part right but don’t know how to do the alternative. This works but, of course, doesn’t show a fallback:

image: page.post_image.toFile

I tried this:

image: page.post_image.toFile.or(page.image.toFile)

but that doesn’t even show the custom image anymore. I also tried

image: page.post_image.or(page.image).toFile

and similar things but the fallback default first image isn’t shown. What am I doing wrong? What’s the default function for retrieving the first image, anyway?

No, that won’t work, you need a page model with a method that returns one or the other. Like the cover method in the Starterkit.

In blueprint:

Wow, the replies come quick here. :slightly_smiling_face:

But I’m probably stupid, I couldn’t get this to work with my setup.

I have a page called ideas.yml containing a file field named “post_image” and have now created a file called “ideas.php” in /site/models. In this file I have:

class IdeasPage extends Page {
	public function previewImage() {
		return $this->content()->get('post_image')->toFile() ?? $this->image();

and in ideas.yml:

image: page.previewImage.toFile

I’ve also tried

  query: page.previewImage

but nothing is shown. :frowning:

This would be the right solution, not the first, because you already return a file from your method.

Could you provide the context for your image query? What is that, a section? If so, I need the complete section definition.

Here is a reduced version of ideas.yml:

        width: 2/3
            extends: sections/ideas
            empty: No posts yet
            status: listed
            headline: Published

the extension:

type: pages
info: "{{'m-d-Y') }}"
template: idea
empty: No posts
sortBy: date desc
  query: page.previewImage

and on the page where the image is coming from (which is a child page of Ideas):

    label: Content
        width: 2/3
        width: 1/3
            type: files
            label: Post Image
            max: 1
            multiple: false
            layout: cards

Is that sufficient or do you need more info?

Your model must be idea.php and class IdeaPagenot ideas, because it is in reference to the child pages, not the parent.

Ah, thank you so much, it works now. Incidentally the same idea crossed my mind but I wasn’t too sure because my PHP skills are pretty limited. Thanks again for your prompt response. :slight_smile: