Jump to content

[solved] Repeaters with images


encho
 Share

Recommended Posts

I am trying to use repeater containing 3 fields, but not really sure how to handle extended information, like image resizing, description, etc.

I have repeater field called 'repeater' with 3 fields: info, file, images. This is my code:

foreach($page->repeater as $field) {
//$image = $field->images;
//$thumb = $image->size(286, 217);

echo "<ul>";
echo "<li><img src='{$field->images->url}' alt='{$field->images->description}' /></li>";
echo "<li>{$field->file}</li>";
echo "<li>{$field->info}</li>";
echo "<li>{$field->images->description}</li>";
echo "</ul>";
}

In above example $field->info, $field->images, $field->file, all produce results, but not sure how to:

1. handle image size (you can see code commented out, tried playing with different variants),

2. get image/file url ($field->images->url doesn't work),

3. same as (2) but file/image description.

I really like the repeater concept, but need to figure out how it actually displays the output. Thanks for all suggestions.

Link to comment
Share on other sites

Hi encho.

If you allow to upload multiple images, then your $field->images returns a WireArray. I think that's causing your problems.

Take a look in the setup of your "images" field, under the Details. If you have the maximum files set to zero, this means no limit:

Description:

Enter 0 for no limit. If you enter 1, the API will dereference this field as a single file rather than an array (when the Page's outputFormatting is on, as it is by default).

Try this code:

foreach($page->repeater as $field) {
//Grab first image of Array... if you set the limit to "1", then you could just write $field->images
$image = $field->images->first();
$thumb = $image->size(286, 217);
echo "<ul>";
echo "<li><img src='{$image->url}' alt='{$image->description}' /></li>";
echo "</ul>";
}

With File Inputfields, it's the same

  • Like 1
Link to comment
Share on other sites

Thank you so much, the code above works perfectly, the only part to change was

<img src='{$thumb->url}' alt='{$image->description}' />

as I wanted to see thumbs.

I just assumed if there's only one image it wouldn't consider the field as an array, your explanation makes much more sense. Thanks again.

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