Jump to content

Getting a parent with a specific template


t0mD
 Share

Recommended Posts

Hi,

I'm wondering if anyone could give me a bit of help, i'm a php newbie, so you mkight need to be patient!

I'm trying to build a navigation menu that's contextual depending on where you are in the site, let me try to explain.

the site tree would be something like this 

Root 

- section

-sub-section

-article

-article

-article

-sub-section

-article

-article

-article

-sub-section

- section

- section

- section

 
 
I need to find a way to get the -section level you are in which always has a different template from the rest (if that makes a difference)
 
I hope this makes some kind of sense.  I've been tying myself in knots trying to come up with different solutions!
 
Many thanks in advance for any help you can give me
Link to comment
Share on other sites

As tsd says, $page->rootParent, assuming it's always the "section" and not the "sub-section" you want. So for the current page, to return the root (root = as far up the tree before hitting the homepage) parent's template you would do:

<?php
echo $page->rootParent->template;
?>

I think if you wanted the subcategory template in your example and the tree could run to any depth, you could do this:

<?php
echo $page->parentsUntil('id=' . $page->rootParent->id)->last()->template;?>

The docs for parentsUntil suggest it will return every parent until the condition is met, so we only want the last one in your case, so hence the usage of last() there.

Funny avatar pic by the way :)

  • Like 1
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

×
×
  • Create New...