$img = $page->$slide()->toFile(); I need to append a value to the end of slide. See code. Thanks

I’m trying to loop through the values so I can add a value to the end of the image. If I echo $slide. It looks like it should pass it into $img A ok. But then nothing shows on the page.

for ($x = 1; $x <= 10; $x++) {
    $slide = 'imageSlide'.$x;
    $img = $page->$slide()->toFile();
     echo $img;


Do I get this right that imageSlide is the name of a field and you have 10 of those fields? And if so, why don’t you use a structure field instead?

There are actually only 4 fields. They are images, we need them to be assigned an order. We’de prefer to put them in a table in the backend.

I think I might have answered my own question. I’ve done this before. I’m not sure why it skipped my mind.

There’s nothing wrong with the code, it just won’t output anything if the fields are empty.

<?php foreach($page->photoGallery()->yaml() as $name) {

    $title = $name['alt'];
    $image = $name['picture'];
  $imageUrl = $page->file($image)->url();
echo html('<h1>'.$title.'</h1><img src="'.$imageUrl.'" alt="'.$title.'">');



So I wrote that and works great, the thing is above it I have to show just the first image. How do I get the first value out of the array out of structured data? Thanks

I think I was overthinking this.


I wouldn’t use yaml() (gives you an array) but to structure() (gives you a collection); makes your life easier. Then you can get the first element using first().

Make sure the image exists before calling the url() method, i.e. use an if statement.

I must be doing something wrong I can’t seem to call outside the loop, just the first image inside the structured data.

Figured it out :slight_smile: Thanks

$item = $page->photoGallery()->picture()->toStructure()->nth(0);
echo $page->contentURL().'/'.$item;

This code looks strange…

$item = $page->photoGallery()->structure()->first();
$image = $item->picture()->toFile();
  echo $image->url();