Jump to content

Getting values from a page reference


Recommended Posts


I must be missing something very basic here.

I have the following code:

$ref_page_id = $page->ref_page;
$ref_page_title = $pages->get($ref_page_id)->title;

But for some (probably a very good one) reason it doesn't work.

In text:

On the current page I have a Page reference fied called ref_page.

When I echo $ref_page_id after the first line, it contains the page id of the reference page. So far so good.

But the second line doesn't produce the correct result. $ref_page_title says Home.

It works when I write the page id manually, like:

$ref_page_title = $pages->get(1243)->title;

What am I missing?


Edit: it does work when I add a second page to the page reference field.

Edited by formmailer
Link to comment
Share on other sites



doesn't return the id, it returns the whole page object (echoes the id though, but var_dumb shows the truth). So no need to get(id), because you already have the correct page. This is what you are looking for:


EDIT: And if you do need the id, it can be get with this:

  • Like 2
Link to comment
Share on other sites

Do you allow one or multiple references? If multiple, then do this: $page->ref_page->first()->title; or then foreach then. If only one is allowed, then set it single page object from field settings and earlier example should work.

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

  • Create New...