Jump to content

Getting children of child with two names


Manaus
 Share

Recommended Posts

Hello, I have a parent holding two children, whose name can be foo|bar. Both can have children, but usually I have foo or I have bar.

What is the selector I have to use when coding in the parent page? I'm trying

$mypages = $page->find('name=foo|bar')->first()->children()

But I get a Call to a member function children() on a non-object. Oddly, I managed to get this selector working

$mypages = $page->get('name=foo')->chilren()

But this is not working when I put a foo|bar OR selector.

Thanks

Link to comment
Share on other sites

1 hour ago, Manaus said:

I'm trying


$mypages = $page->find('name=foo|bar')->first()->children()

But I get a Call to a member function children() on a non-object.

You don't want to chain methods together like that if there is a chance that one of them will return something unexpected. What I mean is if $page->find('name=foo|bar') finds zero pages, then there will be no first() page, and you can't get the children() of nothing.

So you want to break it up a bit more, e.g.

$mypages = $page->find('name=foo|bar');
if($mypages->count) {
    $mychildren = $mypages->first()->children();
    if($mychildren->count) {
        // do something with $mychildren
    }
}

Or seeing as you only want the children of a single page (the first) then you can shorten it a bit by using child() instead of find()...

$mychildren = $page->child('name=foo|bar')->children();
if($mychildren->count) {
    // do something with $mychildren
}

(Actually, $page->find() is different than $page->child() or $page->children() in that it will find pages at any depth under $page, but in your case I think you want to get only a direct child of $page).

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

8 hours ago, chumneypwire said:

If I remember correctly, "get" only returns one page, and not a page array. I believe you would need to do:


$mypages = $page->find('name=foo|bar')->chilren();
foreach($mypages as $mypage) {
	//some logic
}

 

As I read from the Cheatsheet, $page->get returns a field, not a page. Is this correct?

Link to comment
Share on other sites

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...