Jump to content

navigation problem


Raphau
 Share

Recommended Posts

1. Am new to PW and this forum so I'd like to say: Hello to everyone

2. PW is great

3. Got a problem with navigation, I think it should be simple but my brain stopped working :)

here is the situation:

Home

--EN

----ABOUT

----OFFER

------PRODUCT1

------PRODUCT2

--DE

--FR

etc.

What I want to accomplish is to get class="on" added to OFFER when I am on PRODUCT page. Easy to get this done if not using EN, FR pages. Am not using translation functions from PW, languages EN, FR are just a pages.

<!--?php

$lang = $page--->rootParent->name;

$homepage = $pages->get("/".$lang);

echo "

Link to comment
Share on other sites

Hi and welcome to the forums Raphau,

I don't know what complexity has your website, but one of the solutions is to check for substrings: assume two pages:

/xyz/page1
- /xyz/page1/page2

As you can see, parent's url is aways present in childs URL (if only because in processwire we have single-ish page tree). So the condition can be made like this:

if ($page->url === $child->url || $page->parents->has($child))

This checks whether page url is the same as the $child url and if not, checks whether $child is in the parents of the active page

Edit: instead of isValidItem, it should be 'has()'. I fixed it

Ultimately, if your navigation has only two levels, you can go with something simpler:

if ($page->url === $child->url || $page->parent->url === $child->url)

This will check current url against $child, and its parent's url against $child.

Notes:

  • I changed your code to check against URL (in format '/xyz/abc/page-name')
  • I went for typed comparsion (=== vs. ==)
Edited by adamkiss
Fixed code error
Link to comment
Share on other sites

Please, correct me if I'm wrong, but I think that $page->parents->isValidItem($child) only checks wether $child value is an instance of a Page class. And $page->parents->has($child) makes sure that $child value is among the $page's parents.

Link to comment
Share on other sites

Please, correct me if I'm wrong, but I think that $page->parents->isValidItem($child) only checks wether $child value is an instance of a Page class. And $page->parents->has($child) makes sure that $child value is among the $page's parents.

That's correct, you should use has($child). The isValidItem() is meant for internal PW type checking. It only validates that the given object is a Page (any Page).

Sorry, I was checking against docs, and the only function that had Page as parameter was that one, so I got confused here.

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