Page depth below specific parent

I’m building out a kind of microsite that I want to be able to be plugged in anywhere at any page depth and still work. I’ve got some very important help from @texnixe (as seen here Passing field value/variable to child pages), which is awesome.

The new challenge is figuring out how to get page depth but base it on a specific parent page, so that I can have depth-based settings that work no matter what depth the whole sub-structure is located at. This could possibly build off of the previous thread, which tells me which page I have set as my top-level page for this microsite.

What would be really cool is if I could pass a parent page to $page->depth() and get the depth below that parent. But that doesn’t seem to currently be a thing.

I can get a count of parent pages with $page->parents()->count(), but I need a way to stop that at the specific page I’m looking for so I can get depth below that. Any ideas?

$page->parents() gives you an array of all the parents. You can get the key of a specific page using indexOf():


An index of 0 would be the immediate parent of the page etc. You should be able to work it from there, taking the number of elements in the array into account?

1 Like

Excellent. Using that in combination with the page method you provided from the other thread gives me exactly what I need.