Pete Jones Posted April 5, 2016 Posted April 5, 2016 Slight newbie question that is giving me a strange result. I am using a FieldTypePage to include a reference to another page on the site (Case Study with a relevant member of Staff to contact). I have added the field type to my Case Study template and added content and linked it to the chosen member of staff. $contactId = $page->p_contact; echo $contactId; $contact = $pages->get($contactId); echo $contact->title; ContactId correctly returns 1023 but contact->title returns Home, not the title of the corresponding page. Is this an array of some sort starting with Home? How can I call fields in the corresponding page? Do I need to do something with permissions? Many thanks Pete
kongondo Posted April 5, 2016 Posted April 5, 2016 (edited) Have a look at this diagram https://processwire.com/talk/topic/12933-getting-field-name-in-the-loop/?p=117417 // this returns an object; either a Page object or PageArray object // depending on whether p_contact is a single or multiple page field (@see diagram in post I linked to) $contactId = $page->p_contact; // toString() method will return this object's ID (1234) or IDs if multiple page field (1234|3456|6889) echo $contactId; // no need for this. You already have an object $contact = $pages->get($contactId); echo $contact->title; // if p_contact is a single page field echo $page->p_contact->title; // OR $contact = $page->p_contact echo $contact->title; echo $contact->parent->title echo $contact->id; // echo whatever property // if p_contact is a multiple page field (i.e. returns a PageArray) foreach($page->p_contact as $c) echo $c->title; // OR foreach($page->p_contact as $c) echo $c->parent->title; // OR $contacts = $page->p_contact; foreach($contacts as $contact) echo $contact->title; // OR echo $contacts->first()->title; // OR echo $contacts->eq(3)->name; Good practice to always check if your page field returns something (i.e. that it is not empty) before outputting from it // single page field if($page->p_contact)// do stuff // OR if($page->p_contact && $page->p_contact->id > 0)// do stuff // OR for multiple page field if(count($page->p_contact))// do stuff Edited April 5, 2016 by kongondo 3
Pete Jones Posted April 6, 2016 Author Posted April 6, 2016 Hi Kongondo, Thanks for that. Very useful. I did initially try just using it as suggested so perhaps got a field name wrong. I also take on board checking for variables before using them. Cheers Pete
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