Jump to content

Getting the "rootParent"


matjazp
 Share

Recommended Posts

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.

     

Link to comment
Share on other sites

$page->rootParent returns foo of course.

$page->rootParent returns Home of course. :rolleyes:

this should work

        $p = $page;
        while($p->parentID != 1) {
            $p = $p->parent;
        }

Edit: better: Jan Romeros solution (Thanks)

$page->parent("parent=/foo/")
Edited by kixe
Link to comment
Share on other sites

$page->rootParent returns Home of course. :rolleyes:

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 :D

  • Like 1
Link to comment
Share on other sites

$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!

Link to comment
Share on other sites

@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! :rolleyes:
 

@matjazp
You 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
  • Like 2
Link to comment
Share on other sites

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?

  • Like 6
  • Thanks 1
Link to comment
Share on other sites

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);
Link to comment
Share on other sites

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");
  • Like 3
Link to comment
Share on other sites

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"); ?>
Link to comment
Share on other sites

  • 2 weeks 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...