matjazp Posted November 19, 2014 Posted November 19, 2014 I have the structure: Home foo bar1 item1 item2 item21 item22 ... item3 item31 ... bar2 prod1 prod2 prod22 ... ... How to get: bar1 if my current page is item1 or item2 or item21 or item3 or item31 ... bar2 if my current page is prod1 or prod2 or prod22 ... $page->rootParent returns foo of course.
Jan Romero Posted November 19, 2014 Posted November 19, 2014 Try $page->parent("parent=$foo") edit: parent, not parents. 1
kixe Posted November 19, 2014 Posted November 19, 2014 (edited) $page->rootParent returns foo of course. $page->rootParent returns Home of course. this should work $p = $page; while($p->parentID != 1) { $p = $p->parent; } Edit: better: Jan Romeros solution (Thanks) $page->parent("parent=/foo/") Edited November 19, 2014 by kixe
Jan Romero Posted November 19, 2014 Posted November 19, 2014 $page->rootParent returns Home of course. That shouldn’t happen, actually. Does it do that on your site?! It’s supposed to return the parent closest to Home, as matjazp said. I mean, what would be the point of just returning Home for every single page on the site 1
matjazp Posted November 19, 2014 Author Posted November 19, 2014 $page->rootParent returns foo, not home, of course kixe, your solution works, I just replaced your while statement with: while($p->parentID != $pages->get("/foo/")->id) { (actually i just put my foo's id instead of 1). Thank you!
Jan Romero Posted November 19, 2014 Posted November 19, 2014 By the way, there is also closest($foo) and parentsUntil($foo). 2
kixe Posted November 19, 2014 Posted November 19, 2014 @Jan Romero $page->parents("parent=$foo") //or $page->parentsUntil("parent=$foo") will return a pageArray. $page->closest("parent=$foo") will include foo too. $page->parent("parent=$foo") yes thats the best solution. Never used this as a method. Thanks! @matjazpYou should use Jan Romeros Solution like: $page->parent("parent=/foo/") // searching for the name of parent $page->parent("parent=1022") // searching for the id of parent 2
horst Posted November 19, 2014 Posted November 19, 2014 What's about: $page->parent('parent=' . $page->rootParent); // $page->rootParent returns $foo as Page, not name, not id just for the case that the name of foo will change in future, or: what is about usage in a multilanguage site? 6 1
kixe Posted November 19, 2014 Posted November 19, 2014 genious! and $page->closest("parent={$page->rootParent}") will work in bar1 and bar2 too. If you are using the same template.PW API is a creative one. 1
matjazp Posted November 19, 2014 Author Posted November 19, 2014 Wow, lot of responses Let me summarize what works and what doesn't work: // it works $rootParent = $page; while($rootParent->parentID != 1361) { //while($rootParent->parentID != $pages->get("/foo/")->id) { $rootParent = $rootParent->parent; } //doesn' work $rootParent = $page->parent("parent=/foo/"); //doesn' work $rootParent = $page->parent("parent=1361"); //doesn't work $rootParent = $page->parent('parent=' . $page->rootParent); //works $rootParent = $page->closest("parent=$page->rootParent"); //doesn't work $rootParent = $page->parents->eq(1);
kixe Posted November 19, 2014 Posted November 19, 2014 //doesn't workshould work if called in a child page//workseven if called from the page you are looking for 1
Soma Posted November 19, 2014 Posted November 19, 2014 kixie is right. Sorry, just to correct mine it would be index 2, because 1 is root parent and 0 would be root (home). $bar = $page->parents->eq(2); Just to summarize if called from the children // works $parent = $page->parent("parent=$page->rootParent"); // works and same as $parent = $page->parent("parent=1001"); // where 1001 would be /foo/ // works $parent = $page->parents->eq(2); $parent = $page->parents("parent=$page->rootParent")->first; // even works when on the parent itself $parent = $page->closest("parent=$page->rootParent"); 3
matjazp Posted November 25, 2014 Author Posted November 25, 2014 Soma, kixe (and others), thank you, but I don't understand what do you mean by "should work if called in a child page", "even if called from the page you are looking for", "even works when on the parent itself". I'm calling this from my template (template.php) that looks like this: <?php include_once("./functions.inc"); $content = $page->body; include("./default-page.inc"); default-page.inc <?php include("./header.inc"); ?> echo $content; $rootParent = $page->parent("parent=1001"); // where 1001 would be /foo/ echo $rootParent->id; //expected result 1001, it returns 0 <?php include("./footer.inc"); ?>
kixe Posted December 9, 2014 Posted December 9, 2014 The $page object is an instance of the current page, which you are viewing, whereas $template object is an instance of the template itself.
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