Jump to content

parentmost


joe_g
 Share

Recommended Posts

Hi,

how can i get the top level of a certain template? Something like $page->parentMost('template=thing'); perhaps.

Or,

How can I get the top level page under a certain page. I'm looking for rootParent, but relative to a page..?

Like $page->topMost($aTopLevelPage); .. or something..

thanks for any ideas,

J

Link to comment
Share on other sites

Top level page

$page->rootParent

Top level pages of a template

$pages->find("template=thing, parent=1") // 1 is the ID of the homepage, so a child will automatically be the top level page
  • Like 2
Link to comment
Share on other sites

Thanks,

– but rootParent is absolute to the root AFAIK. What I'm looking for is the topmost page right under an arbitrary page. "The top most parent page under page X".

– In your other answer, how do I know what page is the top-most page of the resulting parents?

/J

Link to comment
Share on other sites

Just so we are on the same page understand each other :D

Home

About

Mission

History

1800

1900

2000

rootParent of About = About

rootParent of Mission = Mission

rootParent of History = History

rootParent of 1800, 1900, 2000 = History

$page->parent of About, Mission, History = Home

$page->parent of 1800, 1900, 2000 = History

$page->parents of About, Mission, History = Home

$page->parents of 1800, 1900, 2000 = History, Home

O0

  • Like 2
Link to comment
Share on other sites

"What I'm looking for is the topmost page right under an arbitrary page. "The top most parent page under page X".

Thanks,

– but rootParent is absolute to the root AFAIK. What I'm looking for is the topmost page right under an arbitrary page. "The top most parent page under page X".

– In your other answer, how do I know what page is the top-most page of the resulting parents?

/J

That sounds to me like you are looking for the first child of an arbitrary page - is that right?

  • Like 1
Link to comment
Share on other sites

If that's right it would the the first child or the first child that has children? Not sure. But he's speaking of a "parent"? 

So this would be easy with

$page->child("template=xy, children.count>0");

To get the first child of a page:

$page->child()

will give you the first child with regarding to the sorting.

$page->child("template=sowieso")

same as 

$page->children("template=xy")->first();
  • Like 2
Link to comment
Share on other sites

Sorry for the confusion,

What I'm looking for is how to get the top event (Event3) from Sub-sub-event2. All levels underneath Events are the same template, 'event'.

I cant use rootParent, because that points to Events. If I use parents, how do i know what of the event is the top-most in the result, if they're all the same template?

(Side-note: How fast is parents(), does it recurse upwards for every step, or is it done in one call somehow?)

thanks!

Home
    Events
        Event1
        Event2
        Event3
            Sub-event1
            Sub-event2
            Sub-event3
                Sub-sub-event1
                Sub-sub-event2
Link to comment
Share on other sites

thanks kongondo, but I don't know how many parents are the current page/event. I don't know how many ->parents I would have to put.

I realise I can

1. get ->parents()

2. loop through each result and count(parents) on each one

but this seems slow, I was hoping there was a fast way, similar to rootParent, that is only one call, somehow...

Link to comment
Share on other sites

So are you saying you don't know how many levels deep your events will be going? From your example I assumed you wanted the grand-parent...

Edit:

...but I don't know how many parents are the current page/event.

I don't get this bit "how many parents are the current page/event". Since current page/event cannot be its own parent, did you mean to say.. "how many parents (recursively) the current page/event will have"?

Link to comment
Share on other sites

ehm,

....this was maybe a bit too easy. Maybe that's why it was confusing ;P

But the result seems to be in order of hiearchy, meaning the first result of parents() = top-most

it doesn't mention it in the cheat sheet, but i guess i can count on the order to be a good way to know the top-most page?

Link to comment
Share on other sites

I'm not sure I follow to be honest. Did you find a solution? As you were typing, I tried to think this out logically...technically, what you want is the child (Event3)  of the rootParent (Events) who (the child) is a parent/grand-parent, (has_parent) etc of the sub-events under Event3  :D  O0 Getting that logic in a selector.....

Edit:

Kind related...but about downward ancestry..

http://processwire.com/talk/topic/956-how-to-get-grand-childs/

Link to comment
Share on other sites

Glad you got it sorted Joe. As you rightly suggest, key here is to make sure "Home" and "Event" do not share the same template. Otherwise the code would return home as the first parent.

You may wish to mark this as solved (you answered your own question :D)

  • Like 1
Link to comment
Share on other sites

Glad you got it sorted Joe. As you rightly suggest, key here is to make sure "Home" and "Event" do not share the same template. Otherwise the code would return home as the first parent.

If your Home has the same template as your events you can use this selector:

$parentmost = $page->parents("parent={$page->rootParent}")->first();
  • Like 2
Link to comment
Share on other sites

If Events all have the same template it would be 

If your Home has the same template as your events you can use this selector:

$parentmost = $page->parents("parent={$page->rootParent}")->first();

Which would be the same as

$parentmost = $page->parents->eq(2);

or

$parentmost = $page->parents[1];
  • Like 2
Link to comment
Share on other sites

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