Show number of sub pages on Navigation

Permalink
I searched the forums but could not find anything that related to my issue.

I have a verticle nav on my clients site. It is a online directory, and under each category there are listings. Client wants a number to appear (of the amount of listings in that category) on the nav - next to the name.
for eg

Page Name (22)
Page Name (10)

etc .... etc

can anyone assist in pointing me in the right direction for this?

thanks Maria

agentorange
View Replies: View Best Answer
nebuleu replied on at Permalink Reply
nebuleu
You could probably use the PageList model for that, in a custom AutoNav template.
For example the filterByParentID($cParentID) function.

More on PageList :
http://www.concrete5.org/documentation/developers/pages/searching-a...

Loader::model('page_list');
$pl = new PageList();
$pl->filterByParentID($cParentID);
$number_of_pages = count($pl->get());
bbeng89 replied on at Permalink Reply
bbeng89
You could create a custom template for the autonav and in the loop you could grab the page object with $ni->cObj then get the number of children using the getNumChildren() function. Like so:
foreach ($navItems as $ni) {
   $p = $ni->cObj;
   echo '<li class="' . $ni->classes . '">'; //opens a nav item
   echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '">' . $ni->name . ' (' . $p->getNumChildren() . ') </a>';
   if ($ni->hasSubmenu) {
      echo '<ul>'; //opens a dropdown sub-menu
   } else {
      echo '</li>'; //closes a nav item
      echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
   }
}
nebuleu replied on at Permalink Reply
nebuleu
Forgot this function ...
Have to check twice next time :)
agentorange replied on at Permalink Reply
agentorange
thanks! I tried this and worked great :)
bbeng89 replied on at Permalink Reply
bbeng89
Awesome! If it fixed your problem you should click "mark as best answer." That way, if anyone stumbles on this thread later they can quickly find which answer solved the problem. Thanks!
agentorange replied on at Permalink Reply
agentorange
ok got one more question.... is it possible to only show the number only if sub pages exist? if there is nothing then it shouldnt show (0)
bbeng89 replied on at Permalink Reply
bbeng89
Yeah, I haven't had a chance to test this out yet, but I believe something like this should work:
foreach ($navItems as $ni) {
   $numChildren = $ni->cObj->getNumChildren();
   if($numChildren == 0){ 
      continue; 
   }
   echo '<li class="' . $ni->classes . '">'; //opens a nav item
   echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '">' . $ni->name . ' (' . $numChildren . ') </a>';
   if ($ni->hasSubmenu) {
      echo '<ul>'; //opens a dropdown sub-menu
   } else {
      echo '</li>'; //closes a nav item
      echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
   }
}
agentorange replied on at Permalink Reply
agentorange
Hey

I tired it out and what happens now is that it doesn't even show the pages that don't have children.

Not sure what needs to be done, do you have time to look at it? - not good with coding myself. if I need to pay for work done please let me know. This is a paying client anyways.

Thanks M
bbeng89 replied on at Permalink Best Answer Reply
bbeng89
Ahh, i'm sorry, I misunderstood your question. I thought you didn't want the link to show at all if it didn't have any children. Now I understand though, you just don't want the number to show if it doesn't have any children.

This code should do the trick:
foreach ($navItems as $ni) {
   $numChildren = $ni->cObj->getNumChildren();
   echo '<li class="' . $ni->classes . '">'; //opens a nav item
   echo '<a href="' . $ni->url . '" target="' . $ni->target . '" class="' . $ni->classes . '">' . $ni->name . ($numChildren > 0  ? ' (' . $numChildren . ')' : '') . '</a>';
   if ($ni->hasSubmenu) {
      echo '<ul>'; //opens a dropdown sub-menu
   } else {
      echo '</li>'; //closes a nav item
      echo str_repeat('</ul></li>', $ni->subDepth); //closes dropdown sub-menu(s) and their top-level nav item(s)
   }
}
agentorange replied on at Permalink Reply
agentorange
Thank you so much! it worked superbly!
agentorange replied on at Permalink Reply
agentorange
ok i'm back!

please let me know if there is any cost to this :)

client now wants the top level page to show the total of all the pages (on all levels) that exists beneath it...

any ideas?
bbeng89 replied on at Permalink Reply
bbeng89
Hmm, it's strange because it seems the getNumChildren() method is supposed to return the total number of children in all levels. It looks like the getNumChildrenDirect() function should return only the direct children (the level directly below). So I'm not sure why this isn't working correctly... I keep digging and let you know if I find anyting.