I need to display a different layout based on current pages parent. I’ve tried doing this to load different snippets or to reduce having multiple files load different code within one snippet.
<?php if($page->parent()->title() == 'News'):
// Include the News Article HERO snippet if viewing a news article ?>
<?= snippet('global.hero.news') ?>
<?php else: ?>
<?= snippet('global.hero') ?>
<?php endif ?>
This works perfectly fine until you view a page that has siblings but no parent:
Home > Solutions
Kirby then returns an an error in debug mode:
Error. Call to a member function title() on null
I am using this also for adding a different class name to the body tag:
<?php if($page->parent()->slug() == 'news'): ?>
<body class="hero-<?= $page->parent()->slug() ?>">
<?php else: ?>
<body class="hero-<?= $page->uid() ?>">
<?php endif ?>
With the same results, works fine until a page that has siblings but no parent, the call to member function error.
Any ideas?