Hi,
I’m looking for a convenient way to display the page title in the front-end… in the following logic
- if home page (don’t display)
- if top-level page – other than home (display)
- if children of a subpage (display parent page title)
here is a raw visualization of what I’m trying to achieve
Is there something that Kirby 3 offers for such a scenario in terms of convenience?
<?= ( $page->isHomepage() === false && ! $page->hasParent() ) ? $page->title()->html() : $page->parent()->title()->html(); ?>
Probably missing the homepage option but you get the idea…
Thanks @pixelijn
I got the idea but had problems with
! $page->hasParent()
Looks like this condition is applied even when I browse the “Blog” page and I get the following error
Call to a member function on null
<?= $page->parent()->title()->html(); ?>
if I visit the “article” page this works like a charm
Looks like this might have a similar issue as described here.
I got things working with $page->parents()->count()
and solution looks like this
<?php if( $page->isHomePage() === false && ! $page->parents()->count() ) : ?>
<?= $page->title()->html(); ?>
<?php elseif( $page->isHomePage() === false && $page->parents()->count() ) : ?>
<?= $page->parent()->title()->html(); ?>
<?php else : ?>
// Do nothing
<?php endif ?>
Really like the shorthand version but I have no idea how to add one more if statement
Since nested ternary operator are not really recommended, better go for the if statements.
Good to know. I think this is solved now.