Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » How to count all Children of a page? Including those not shown in menus

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1776 Views
  • Tama
    Avatar
    Community Member
    130 Posts

    How to count all Children of a page? Including those not shown in menus Link to this post

    Hi there

    I'm writing a function which has to work out how many children a page has. I started off with this code:

       public function numChildren()
       {
          $children = $this->Children();
          return $children->Count();
       }

    However this doesn't count the child pages that have "Show in menus" unticked.

    Does anyone have a simple solution to this?

    Cheers
    Tama

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: How to count all Children of a page? Including those not shown in menus Link to this post

    Have you tried $this->AllChildren() ?

    That should do the trick.

    There's not much in the docs but you can use $AllChildren.Count directly in the template if all you want to do is output the number

    hth

    Rich

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: How to count all Children of a page? Including those not shown in menus Link to this post

    Excellent, that was exactly what I was after - thank you Rich.

    What I am doing it putting in a RSS link on every page (picked up by modern browsers) but if the page hasn't got any children it points back to the parent page RSS link.

    class Page_Controller extends ContentController {
       public function init() {
          parent::init();
          
          //Display RSS link on every page with children. If no children display parent RSS link
          if($this->numChildren()>0){
             RSSFeed::linkToFeed($this->Link() . "rss");   
          } else {
             RSSFeed::linkToFeed($this->Parent()->Link() . "rss");   
          }
       }
       
       public function numChildren()
       {
          $children = $this->AllChildren();
          return $children->Count();
       }

       function rss() {
          $rss = new RSSFeed($this->Children(), $this->Link(), $this->Title." RSS Feed");
          $rss->outputToBrowser();
       }

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: How to count all Children of a page? Including those not shown in menus Link to this post

    As a little note using $this->AllChildren()->Count is quite slow since it has to build a whole set of the pages just to get the count. If you are worried about performance then a direct count db query would be much faster. Like DB::query("SELECT COUNT(*) FROM SiteTree_Live WHERE ParentID = '$this->ID'")->value();

  • TotalNet
    Avatar
    Community Member
    181 Posts

    Re: How to count all Children of a page? Including those not shown in menus Link to this post

    Glad to help.

    In that case you can save yourself a function and just put:

    ...
    if ($this->AllChildren()->Count()>0) {
    ...

    a word of warning on using "Show in Menus", Google site maps uses this as an extra selector for deciding whether to include the page in sitemap.xml - just something to consider

    Rich

    EDIT: or use Willr's snippet ;)

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: How to count all Children of a page? Including those not shown in menus Link to this post

    Thanks again - I might take Will's advice and go for the direct SQL query. Speed is good.

    1776 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.