Testing specific page ID for child pages

Permalink 1 user found helpful
Hi Guys

I am hard coding in a dropdown navigation and for each link I want to test if it has child elements so I can add in the dropdown menu and display the child links.

I've got the displaying child links worked out. What I can't quite grasp is how I can say 'here's a page ID, does it have children?'.

I have been tring this method:
$page->getNumChildren()


But I can't seem to find anyway to pass it an ID and the documentation is sparse.

Can any clever souls suggest a way of doing this?

thanks drongo (falling asleep at his desk :/ )

 
mhawke replied on at Permalink Reply
mhawke
Try this:

$page = Page::getCurrentPage();
echo $page->getNumChildrenDirect();


or this:

$page = Page::getCurrentPage();
echo $page->getNumChildren();
Jebediah replied on at Permalink Reply
Hi mhawk

Thanks for getting back to me. I still remember how u solved my last issue :).

I am not sure the above would work as I need to set the dropdown even if the user is not on the current page.

So what I was hoping for was a method that would return the number of child pages based on whatever page id I supply.
mhawke replied on at Permalink Best Answer Reply
mhawke
Sorry, I misunderstood. Try this instead:

$cid = 1; //cID of home page, for example.
$p = Page::getByID($cid);
echo $p->getNumChildrenDirect();
Jebediah replied on at Permalink Reply
Thank you mhawke.

That's precisely what I needed and it worked perfectly. You are my saviour once again :)