Jump to content

How to find the child of a child?


adinuno
 Share

Recommended Posts

Hi there,

First of all, I am sorry for the spam of small questions, but as they are all very simple and fit for my problems, they can as well fit for someone else's problems too.

I am testing around the $page->children function and I wanted to find all the children inside the children of the page's first child.

It is a bit complicated to explain, but I'll illustrate the situation:

first level

- > second level

- - - > third level

- - - - - - > fourth level

- - - > third level

- - - - - - > fourth level

- - - - - - > fourth level

- - - > third level

If we read this list as a page tree, I want to find the url of each "fourth level" page.

I have tried the most logical yet with no success:

<?php foreach( $page->child->children->children as $children) { ?>
   <h1><?php echo $children->url ?></h1>
<?php } ?>
 

What am I doing wrong?

In JQuery terms I could navigate through my levels with the $(this).parents()[5] function, but Processwire seems to lack this capability.

Thanks in advance

Link to comment
Share on other sites

That "$page->child->children->children" is the issue here; $page->child is a Page, $page->child->children is a PageArray and you can't ask for children of a PageArray.

PageArray contains Pages, each of which has children -- PageArray itself doesn't have children. You'll have to iterate over all contained pages one by one and ask for their children :)

<?php
foreach ($page->child->children as $children) {
    foreach ($children->children as $child) {
        echo "<h1>{$child->url}</h1>";
    }
}
?>

Does this make sense to you?

  • Like 3
Link to comment
Share on other sites

That "$page->child->children->children" is the issue here; $page->child is a Page, $page->child->children is a PageArray and you can't ask for children of a PageArray.

PageArray contains Pages, each of which has children -- PageArray itself doesn't have children. You'll have to iterate over all contained pages one by one and ask for their children :)

<?php
foreach ($page->child->children as $children) {
    foreach ($children->children as $child) {
        echo "<h1>{$child->url}</h1>";
    }
}
?>

Does this make sense to you?

Yes it does, I actually thought the same way before and tried to do it, but I must've messed up my logic.

That works and is very simple to understand, pure javascript logics.

Thank you very much ;)

Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...