Jump to content

page field values


opalepatrick
 Share

Recommended Posts

I will get the hang of this eventually.

I am trying to get a value(s) from a page field that could have multiple values as it used for a multi select field.

foreach($page->pattern_type->title AS $pt){
   echo $pt;
}

This is giving me nothing and no error either. Just the $page->pattern_type->title give me nothing either.

Any help appreciated.

Link to comment
Share on other sites

$page->pattern_type should return an instance of PageArray, which won't have field "title" -- or any other fields for that matter. PageArrays are collections of Pages, and Pages are the ones with fields (and in any case you wouldn't be able to iterate title field as in your example).

Try iterating Pages contained in that PageArray, like this:

foreach ($page->pattern_type as $pt) {
    echo $pt->title;
}
Link to comment
Share on other sites

I doubt "title" is an array, though you may have an array made up of titles.

Assuming pattern_type is you page field, then that is your array of values, including title and any other field involved.

So:

foreach($page->pattern_type as $pt){
echo $pt->title;
}

EDIT: Beaten by Teppo!

Link to comment
Share on other sites

hah!

It can get a bit brain numbing by the time you have a page field calling a page with a repeater in it that includes a multiple image field. ....

The trick is to basically think of it as a tree of info where vertically is the relationship and horizontally is the data.

So

$page is my current page and therefore the top of my little tree

It has a page field in it called mypagefield, So to get to that

$page

-> mypagefield

mypagefield includes the data from more than one other thing, therefore it must be an array. That goes out horizontally:

-- mypagefield = title, title, title, title, and so on. (it may include other fields too)

So, that is the tree. The array of data we need is contained in mypagefield so we would write it as

$page->mypagefield

Then we can do what we like with it.

If that array included arrays of information like an image field, for instance, then we have to work our way through one array and then another

foreach($page->mypagefield as &pagefield){

// grab each title as an example
  echo $pagefield->title; 

// Now we need to go through the array of images that is in a field called "images"

      foreach($pagefield->images as $image){
         echo "<img src='{$image->url}'></br>";

      } //end the image foreach

} //end the page field foreach

So, we have gone through one array, and since that array contains another array, we have gone through that as well. But as you can see it is a logical order. 

As I always say, if in doubt, work it out visually with a pencil and paper - it can un-fog the brain nicely.

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

  • Recently Browsing   0 members

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