Jump to content

Root page and $page->id


mmc
 Share

Recommended Posts

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

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 ...
   }
}

 

  • Like 1
Link to comment
Share on other sites

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

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...