Jump to content

Get first image from a repeater field on another page and resize it?


Lance O.
 Share

Recommended Posts

I want to resize an image that is included in a selected featured project as referenced from a Home page.

The "Featured Project" field found on the Home page is using a Page type. The "Images" field included in the "Featured Project" is a repeater field that includes an Image field.

How do I access the first image in the "Images" field and also resize the image?

This works and returns the ID of the image:

$img = $page->featured_project->images->first();

but this doesn't work to resize the image:

$img = $page->featured_project->images->first();
$thumbnail = $img->size(236,225);

Nor does this:

$img = $page->featured_project->images->first()->size(236,225);

Any insight is appreciated!

Link to comment
Share on other sites

This works and returns the ID of the image:

When you say "ID" of an image, is that a number? The reason I ask is because images don't have an ID. They are referenced by their filename, and that is their "ID". So if you are seeing a number when accessing your field, chances are you are really accessing a Page (or repeater item page) rather than an image. 

Link to comment
Share on other sites

@ryan

You are correct. The following code returns the ID of the repeater item page, not an image:


$img = $page->featured_project->images->first(); echo $img;

Is it possible to retrieve and resize an image that is included in a repeater field from another page?

Link to comment
Share on other sites

What's the name of the image field inside the repeater? Assuming it's "image_field" for example, your code should look like this
 

$img = $page->featured_project->images->image_field->first()
Link to comment
Share on other sites

I just deleted all of my existing fields and started from scratch. This definitely does not work:

$img = $page->featured_project->images->image_field->first();
echo $img;
The error message:
Error Call to a member function first() on a non-object
Link to comment
Share on other sites

What are the settings under details for the featured_project field in the admin? 

The "Featured Project" field found on the Home page is using a Page type. The "Images" field included in the "Featured Project" is a repeater field that includes an Image field.

You state that the images field is included in the page reference field. Do you mean that the images field (a repeater) is a field assigned to a template that is used on a page you referenced with the featured_project page field type?

Sorry but I am also not clear on your field names.  Could you tell us what are the field names of the repeater field and the image field assigned to the repeater field. Also what are the settings for the image field , multiple images or just 1?  I will try to help you work through this if I can. 

Link to comment
Share on other sites

@JeffS

The field type for "featured_project" is set to: Multiple pages (PageArray).

Note that for the solutions posted below, the field type for "featured_project" was set to Single page (Page) or empty page (NullPage) when none selected.

Here's the set up:

Home page

-- featured_project (Page field)

Project page

-- images (repeater field)

---- image_field (image field)

Edited by Lance
Link to comment
Share on other sites

sounds like you have to maybe first setup a loop for the featured projects?, and then setup a variable to hold each of the featured projects..something like this?

$featured = $page->featured_project;
foreach ($featured as $feature) { 
 $img = $feature->images->first(); 
echo $img
}
Link to comment
Share on other sites

Thanks @diogo, @ryan, @JeffS, and @Macrura73. I appreciate the help.

I've tried the suggestions here, as well as many of my own, but still no luck. These are two solutions that seem to work, but are more verbose:

$first = true;
foreach ( $page->featured_project->images as $image ) {
	if ( $first ) {
		$thumb = $image->image_field->size( 100, 100 );
		echo '<img src="' . $thumb->url . '" alt="' . $thumb->description . '" width="' . $thumb->width() . '" height="' . $thumb->height() . '">';
		$first = false;
	} else {
	}
}

Another solution:

$i = 0;
foreach ( $page->featured_project->images as $image ) {
	if ( $i == 0 ) {
		$thumb = $image->image_field->size( 100, 100 );
		echo '<img src="' . $thumb->url . '" alt="' . $thumb->description . '" width="' . $thumb->width() . '" height="' . $thumb->height() . '">';
	}
	$i++;
}

To summarize, the Home page includes a field called "featured_project." In these solutions, the "featured_project" field type is "Page" and is set to "Single page (Page) or empty page (NullPage) when none selected" on the field's Details tab. A featured project contains a repeater field named "images" and contains an image field named "image_field."

Link to comment
Share on other sites

since i've done this kind of thing a lot, the first thing i notice about your setup is that you are using an image field within a repeater, but the image field already supports multiple images, so if i was doing this, i would not use a repeater, but just a plain image field, that allows multiple images.

if you are only going to have 1 featured project, then this should work, assuming you don't put the image_field inside a repeater:

$featured = $page->featured_project;

$img = $featured->images->first();

$thumb = $img->size( 100, 100 );

  • Like 1
Link to comment
Share on other sites

I agree with Macrura73 that you don't want to use a repeater for images unless you need more metadata than what the images fieldtype provides. This is also a bit confusing from the code side because 'images' is representing a repeater rather than an images field. 

But sticking what what you've already got -- based on the examples that you say do work, you should be able to reduce it to this below. I'm also assuming that image_field is set to contain a maximum of 1 image. If not, the code would be a little different. 

if($page->featured_project->id) {
  // $page has a featured project
  $item = $page->featured_project->images->first(); // $item is repeater item
  if($item->id && $item->image_field) {
    // there is an item, and it has a populated image field
    $thumb = $item->image_field->size(100,100); 
    echo "<img src='$thumb->url' alt='$thumb->description' width='$thumb->width' height='$thumb->height' />";
  }
}
Link to comment
Share on other sites

@Macrura73

I see your point. That's a subtle distinction that makes a big difference. In this case, I don't necessarily need to use a repeater, but I can see cases where I would. I've tested your code and it works flawlessly. Thank you for your eye for detail!

@ryan

Your example code works perfectly! Thanks for taking the time to work this out.

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

×
×
  • Create New...