Background image from sub page

Hi, I have created an overview project page and here I am displaying the title, excerpt and now the image as a background image of a divider. I have referenced an image similarly on another page, but this time I am looking to get the first image inside the project child page.

<div style="background-image:url(<?= ($f = $project->image()->toFile()) ? $f->url() : ''; ?>)">

What is the main image?

Your code won’t work, because $project->image() returns a file object, not a field object, so you cannot call toFile() on this.

I am assuming that you know what objects, classes and there methods are?

Hi, by main image, i meant first image in the folder. So in this case can I use toField()

Nope just

<div style="background-image:url(<?= ($f = $project->image()) ? $f->url() : ''; ?>)">
1 Like

Worked like a charm, thank you so much!