Jump to content

Creating new FieldsArray or filtering $page->fields


Recommended Posts

Posted

Using $page->fields makes it easy to output field labels and values for a page in a foreach loop, which is great when you have fields that you want to output with the same markup structure.

But what if I don't want all the fields in the page, but just some of the fields, so I can use two or three different foreach loops for different markup needs.

The docs say that $page->fields returns a "FieldsArray". How can I create my own FieldsArray, selecting fields by name?

Or maybe filter $page->fields by field name?

Posted

You can filter like this:

foreach($page->fields->find("type=FieldtypeImage") as $f){

or you can create a new field array with:

$myFields = new FieldsArray();
foreach(xxxx as $f){
    $myFields->add($f);
}
  • Like 1
Posted

Thanks! I was hoping there was some sort of find method for page fields, and of the two options I'll probably use this.

But for

$myFields->add($f);

...if I know the name of the field I want to add to the FieldsArray, how do I get the item into the right type for adding to a FieldsArray?

Edit: worked it out now... :)

$myFields = new FieldsArray();
$myFields->add($page->fields->get("fieldname"));
  • Like 1

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...