Manaus Posted May 2, 2018 Share Posted May 2, 2018 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 More sharing options...
chumneypwire Posted May 2, 2018 Share Posted May 2, 2018 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 } Link to comment Share on other sites More sharing options...
Robin S Posted May 2, 2018 Share Posted May 2, 2018 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). 2 1 Link to comment Share on other sites More sharing options...
Manaus Posted May 3, 2018 Author Share Posted May 3, 2018 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 More sharing options...
ottogal Posted May 4, 2018 Share Posted May 4, 2018 On 5/2/2018 at 10:32 PM, Manaus said: $page->get('name=foo') To get a page you'd need $pages->get('name=foo') Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now