How to print field of page or parent page (if it's in parent page)?

<?php
$background = $page->background()->toFile() ?? $page->parent()->background()->toFile();

if ($background): ?>
                    style="
                    background:url(<?= $background->url() ?>);
                    background-position: center center;
                    background-size: cover;
                    "
 <?php endif ?>

Never call a method (in this case url()) without asserting the object exists.

1 Like