My pages have an hero image. When no image is chosen the parent page should be checked for an hero image. If that’s not the case, the parent parent page should be checked, and so on. If there’s no parent page(s) a fallback image should be used.
The following codes works, but a) this is very likely not the most elegant solution and b) I also want this to work for any page depth (subpage level) without blowing the code up even more.
Any ideas? THANKS!
<?php if($image = $page->hero()->toFile()): ?>
<img srcset="<?= $image->srcset('header') ?>" sizes="100vw" src="<?= $image->thumb('header')->url() ?>">
<?php elseif($page->depth() === 2 AND $image = $page->parent()->hero()->toFile()): ?>
<img srcset="<?= $image->srcset('header') ?>" sizes="100vw" src="<?= $image->thumb('header')->url() ?>">
<?php elseif($page->depth() === 3 AND $image = $page->parent()->hero()->toFile()): ?>
<img srcset="<?= $image->srcset('header') ?>" sizes="100vw" src="<?= $image->thumb('header')->url() ?>">
<?php elseif($page->depth() === 3 AND $image = $page->parent()->parent()->hero()->toFile()): ?>
<img srcset="<?= $image->srcset('header') ?>" sizes="100vw" src="<?= $image->thumb('header')->url() ?>">
(...)
<?php else: $image = asset('assets/img/header-fallback.jpg'); ?>
<img srcset="<?= $image->srcset('header') ?>" sizes="100vw" src="<?= $image->thumb('header')->url() ?>">
<?php endif ?>