manlio Posted September 4, 2014 Share Posted September 4, 2014 Im' building for exercise pourpose a simple menu system based on pages. Something like menu -menu1 -menu2 -menu3 I have build a menu template with a page field that I would like to use in the href attributes of menu link. Everything else works, but I don't know how to retrieve the url of the related page and not the ID. foreach($menus as $menu) { echo $menu->title echo $menu->pagereference; //this return the ID of the related page, but I would like to get the url ?> Something like this $menu->pagereference->url doesn't work so I don't know the syntax, I have tried different ways with no success. Thank you and sorry for the silly question. Regards Link to comment Share on other sites More sharing options...
adrian Posted September 4, 2014 Share Posted September 4, 2014 If it's a multiple select page field, then you either need to convert it to single select, or grab the first one: $menu->pagereference->first()->url; 1 Link to comment Share on other sites More sharing options...
manlio Posted September 4, 2014 Author Share Posted September 4, 2014 Thank you adrian, unluckily this doesn't solve my problem. I need only one page and before I setted up the field as Single Page. I try to put back to multiple and use your syntax but still I get this error: Notice: Trying to get property of non-object in ...\site\templates\header.inc on line If I leave the field to Single Page and try to use echo $menu->pagereference->url; I get the same error. Link to comment Share on other sites More sharing options...
adrian Posted September 5, 2014 Share Posted September 5, 2014 Sorry that didn't work. I think I must not be fully understanding the setup you have, because that should work. Can you please show us the selector you are using to define $menus ? Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 5, 2014 Share Posted September 5, 2014 Maybe there is a page sneaked up in you menu's that hasn't the right template. 1 Link to comment Share on other sites More sharing options...
Wanze Posted September 5, 2014 Share Posted September 5, 2014 This sounds strange. What do you get when outputting var_dump($menu->pagereference);die(); Should be a Page object with lots of information... what does it say in your case? 1 Link to comment Share on other sites More sharing options...
manlio Posted September 5, 2014 Author Share Posted September 5, 2014 Thanks a lot to everybody! You all was really helpful. I think in one page I didn't set the pagereference and this cause the error (still not sure). But now thanks to your advice works perfectly. Great forum and PW rocks! Edit: Previously I setted the Page as "Single page (Page) or boolean false when none selected" instead of "Single page (Page) or empty page (NullPage) when none selected" and now works also if a page reference is blank 1 Link to comment Share on other sites More sharing options...
Wanze Posted September 5, 2014 Share Posted September 5, 2014 If you'd choose "Single page (Page) or empty page (NullPage) when none selected" in your pagereference field, then the error wouldn't happen. However, maybe you should modify your snippet to check if there is a page selected before outputting the url 3 Link to comment Share on other sites More sharing options...
manlio Posted September 5, 2014 Author Share Posted September 5, 2014 Wow, just found this by myself, but thank you for your excellent support! 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