Check if a page is descendant of any page

Hi,

Is it possible to check if a page is a descendant of any page?

<?php if ($page->isDescendantOf('blog')): ?>

Wondering if it’s possible to do something like:

<?php if ($page->isDescendant()): ?>

Maybe the https://getkirby.com/docs/reference/objects/page/depth method is what you’re looking for?

Ha! First time I ever beat you, @texnixe :racing_car:
Seems like there are multiple ways to check if a page has parents…

2 Likes

You can check with $page->parents()->count(). @thguenther was faster…

1 Like

Awesome! And congrats @thguenther for the #1 win!

2 Likes

On a side note, I have a collection of custom page methods here: https://github.com/texnixe/k3-pagemethods/blob/master/index.php, that includes a hasParents() method.

It might be worthwhile to create your own set of methods that you find you need more often and you find missing in Kirby.

1 Like