mmc Posted March 6, 2018 Share Posted March 6, 2018 I'm using in my current project the following snippet. $root = $pages->get("/"); $children = $root->children(); $children->prepend($root); foreach($children as $child) { DoSomething1 ... if ($child->numChildren(true) && $child->id > 1) { DoSomething2 ... } } Is it safe to exclude the root page from the DoSomething2 branch with $child->id > 1 ? Or in other words, does the root page always has the ID 1 ? Link to comment Share on other sites More sharing options...
dragan Posted March 6, 2018 Share Posted March 6, 2018 14 minutes ago, mmc said: does the root page always has the ID 1 ? Always. 1 Link to comment Share on other sites More sharing options...
Robin S Posted March 6, 2018 Share Posted March 6, 2018 Alternatively, you already get the root page as an object in the first line of your code, so you can compare $child to $root and exclude it that way. $root = $pages->get("/"); $children = $root->children(); $children->prepend($root); foreach($children as $child) { // DoSomething1 ... if ($child->numChildren(true) && $child !== $root) { // DoSomething2 ... } } 1 Link to comment Share on other sites More sharing options...
mmc Posted March 7, 2018 Author Share Posted March 7, 2018 11 hours ago, Robin S said: Alternatively, you already get the root page as an object in the first line of your code, so you can compare $child to $root and exclude it that way. Indeed, very good point. I missed this completely. Code changed, thanks. 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