Absolutely not sure about this but I think you can use some sort of recursive function.
function checkChildren($initialPage) {
if (!$initialPage->hasChildren()) :
// Do Something if you reached the end of the tree
else :
foreach ($initialPage->children() as $subPage) :
checkChildren($subPage);
endforeach;
endif;
}
foreach ($site->children() as $entry) {
checkChildren($entry);
}
I’d like to add that the final foreach may be redundant. If I understand it correctly, every page is a subpage of $site isn’t it ?
Then wouldn’t it give the same results to start iterating $site directly using your function, instead of using a foreach to get $site’s children, then iterate each children using your function? I mean:
checkChildren($site);
…instead of:
foreach ($site->children() as $entry) {
checkChildren($entry);
}
Because I did not reply to this I probably went for a different solution. Nowdays I’m much better at recursive functions and snippets as I need it in my plugin that I’m developing right now.
Anyway, it looks alot like @manuelmoreale suggestion, but like @plagasul said, no loop before the function call is needed if done well.