Jump to content

Is descendant?


Sinmok
 Share

Recommended Posts

Hello all,

Probably a silly question..but I'm having trouble figuring out if a page is a descendant of another page.

Let's assume I've got a page currently referenced as $section. I need to know whether $page (current page) is a descendant of $section, preferably returning a true/false value.

Does anyone know how to accomplish this?

Something like this would be solid:

if($page->isDescendant($section)){
  //$page is a descendant of the $section node
}

//OR

if($section->hasDescendant($page)){
    //$section is an ancestor of $page
{

EDIT:

In the end, I got it working using the following code.

$page->parents("id=$section->id")->first()->id == $section->id

This is probably not the best of solutions though so I'm interested in hearing yours.

Cheers

Link to comment
Share on other sites

My take

if($page->parents->has($section)) ...

if($section->find("id=$page")->count()) ...

// direct parent-child

if($section->children("id=$page")->count()) ...

if($page->parent === $section) ...
  • Like 5
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...