Jump to content

Noob question: Problem with page field used as link


Recommended Posts

Posted

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
Posted

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

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.

Posted

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 ?

Posted

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?

  • Like 1
Posted

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

  • Like 1
Posted

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

  • Like 3

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...