How to test if page has children

Is anyone aware of a way to test within a template or autonav whether or not the current page has any children. Here's the scenario - I have a side bar auto nav hardcoded into my template - it shows the relevant subpages of whatever top level page you choose. The problem is, I have one or two pages that have no subpages, and rather than having a blank spot where the autonav normally is, I'd like to programatically insert some other content.

The closest thing I've found is the hasChildren() function in the autonav controller, but I haven't found a way to use that within my template php.

Any ideas?

View Replies: View Best Answer
beebs93 replied on at Permalink Best Answer Reply
Each Page object has a "getNumChildren" method that simply returns the public "cChildren" property.

global $c;
$intChildPages = $c->getNumChildren();
// or
$intChildPages = $c->cChildren;

This can be run almost anywhere; template files included.

Generally, this is an accurate number of child pages under the Page object, but it doesn't take into account viewing permissions, system pages, etc.
hursey013 replied on at Permalink Reply
Thanks that worked great.

I realized that this might actually be more complicated though. Are you aware of a way to tell is you are on a top level page? Basically what I'm looking to construct is an if statement that checks first of all whether we are on a main top level page, if so whether or not it has any children. The getNumChildren() does the second part great - the issue is that once you are actually on a child page it then see there are no children under that child and hides the menu.

Probably making this more complicated than it needs to be - but I appreciate your help.
beebs93 replied on at Permalink Reply
Look at the "getTrailToCollection" method in the Navigation helper (

It returns an array of breadcrumb Page objects so using that you can see which ancestor pages are above you at any time.
aghouseh replied on at Permalink Reply
getCollectionAncestor! Did that ever get pulled? :/
beebs93 replied on at Permalink Reply
Heh, I almost pasted it in my previous response (and no, it stayed untouched for a while so I removed the pull request).

 * Retrieves a specific ancestor Page object (not including the home page) of any descendant Page object
 * @param object Page $cObj - Starting descendant Page object to check from
 * @param integer $nth - The nth ancestor to retrieve starting from the descendant page. Omit to retrieve the highest ancestor
 * @return object - Page object
public function getCollectionAncestor($cObj, $nth = NULL){      
   // We return null if passed argument is not a valid Page object, if we're on the home page, a system page, a master collection or the current highest ancestor
   if((!is_object($cObj) || !$cObj instanceof Page || $cObj->error) || $cObj->cID === HOME_CID || $cObj->isSystemPage() || $cObj->isMasterCollection() || $cObj->getCollectionParentID() === HOME_CID){
      return NULL;
   // We return our current Page object if we haven't requested any ancestors
   }elseif($nth === 0){
      return $cObj;
   // We return the parent Page object if we only want the immediate ancestor