John W. Posted September 21, 2019 Share Posted September 21, 2019 Working on a project where I have different categories of Departments, under departments I have Offices, and each office has multiple pages. DEPARTMENTS HUMAN RESOURCES About Human Resources Job Listings job 1 details page job 2 details page ADMINISTRATION About Administration Meet the Administrator ----------------------------- stored in HUMAN RESOURCES is a background image and title of the department. Instead of creating a background image for each office page (and it's sub pages like job 1 details page), I'm using the background image stored in HUMAN RESOURCES for all the child pages. I'm familiar with using $page->rootParent, however, this will return DEPARTMENTS rather than the office, e.g. HUMAN RESOURCES that each sub page belongs to. I thought about using a recursive function to traverse from the current page, when it reaches the template "office". Basically, if I'm in job 1 details page, it would traverse back up the tree until it reaches "office" template, such as "HUMAN RESOURCES". Has anyone done something similar, but, using a built in method that I'm missing? Thanks. Link to comment Share on other sites More sharing options...
ottogal Posted September 21, 2019 Share Posted September 21, 2019 Suggestions: You could identify the HUMAN RESOURCES page as the first child of the root page: $page->rootParent->children()->first() Or: Add the background image to the rootPage and refer to it from the Office pages. Link to comment Share on other sites More sharing options...
John W. Posted September 21, 2019 Author Share Posted September 21, 2019 Yeah, tried that, it pulls the first child under DEPARTMENTS This doesn't work when you have several departments such as DEPARTMENTS HUMAN RESOURCES About HR Job Listings Job 1 Job 2 ADMINISTRATION About Administration EMERGENCY OPERATIONS About Emergency Operations In the case above, your suggestion would always return "Human Resources" Link to comment Share on other sites More sharing options...
John W. Posted September 21, 2019 Author Share Posted September 21, 2019 I think I found a quick solution: # cycles through all the current $page parents then # returns the parent that is #x in the list # $page is the current page, $parentNumber is the # parent to find and return in the tree function getParent($page, $parentNumber) { $cnt=0; foreach($page->parents() as $p) { $cnt++; if($cnt == $parentNumber) { return $p; } } return false; } #pass the current $page and return its third parent $parentPage = getParent($page,3); echo $parentPage->title; Link to comment Share on other sites More sharing options...
adrian Posted September 21, 2019 Share Posted September 21, 2019 Couldn't you use this, assuming all offices, eg HUMAN RESOURCES have a template names "office" $page->parents('template=office'); Actually you might want "parent" instead of "parents" to make it easier in this case. 4 Link to comment Share on other sites More sharing options...
dotnetic Posted September 22, 2019 Share Posted September 22, 2019 Use Adrian's solution 1 Link to comment Share on other sites More sharing options...
bernhard Posted September 23, 2019 Share Posted September 23, 2019 $page->closest() might be the slightly better option (or might not): https://processwire.com/api/ref/page/closest/ 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now