Jump to content

numChildren vs children?


evanmcd
 Share

Recommended Posts

Hi,

I'm having the strangest problem, that I'm guessing someone will look at and figure out in two seconds.  It's been that kind of day...

I'm simply trying to loop through the children of a page using the code below:

$my_page = $pages->get("/my-page/");
echo('<p>$my_page->numChildren: ' . $my_page->numChildren . '</p>');  // 84
$my_children = $my_page->children;
echo('<p>count($my_children): ' . count($my_children) . '</p>'); // 0
foreach($my_children as $child) {
    echo('<p>Looping on $child</p>'); // never displayed
}

What I'm getting is not making sense... why would numChildren return the correct number of children of the page, but $page->children wouldn't?

Thanks.

  • Like 1
Link to comment
Share on other sites

Sorry . Damn mobile reading i missed it. Ok but the answer to your problem is: You have to write children() as it's a method not a property. Some other API can be used both with and without so but in this case i think it doesnt. Maybe still something for Ryan to change in case it's this.

Damn again, I shouldn't call from memory when having sleep loss. :( I think Wanze has a good point below :)

  • Like 2
Link to comment
Share on other sites

$my_page->numChildren does also count hidden (maybe unpublished) pages, while $my_page->children does not include them by default. If you use count($my_page->children("include=hidden")), it's the same number

Edit: Too late again :grin:

  • Like 4
Link to comment
Share on other sites

Oh, of course, the children are all unpublished.  "Why couldn't you easily tell that they were unpublished by the strikethrough" you ask?  Because I'm using a slightly modified version of @Soma's nice PageListImageLabel module that has images shown as dimmed when they are published but fully opaque when they are unpublished (for moderation of the images).

Thanks @Wanze and everyone for the help.

Oh, and of course special thanks to @Adamkiss for the "hug".  Those guys are only slightly balder than I am :)

  • Like 2
Link to comment
Share on other sites

  • 2 years later...

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...