Jump to content

[Solved] How to get an image from the elements of a $pages->find array?


Recommended Posts

Posted

I have been trying everthing about images. I have read the documentation about the field itself, some threads, a tutorial, and the cheatsheet.

I am creating an events portal, and I have a template called Event, with an Image field for the poster of the event.  In the homepage I am listing the events, and I am able to output the Tiitle and the Date (with is another field), but I am not able to output the poster.

How can I do this? I am missing something? If you need any other information, just tell me.

Thanks in advance!

Posted

The image field default setting is to output an array, so if you only want to output one image of the page, try on your loop:

$event->images()->first()->url;

 

  • Like 3
Posted

Thank you very much, but it doesn't work for me. It works for the $page variable (the homepage), but it doesn't work in the single $event of my loop (that is a foreach).

What can I been doing wrong? :(

By the way, it seems that images of $page is a property and not a method.

Posted

@Elías Gómez, can you post the code you are using to loop over your event pages? It's hard to offer much advice without seeing that. Also let us know what you have selected for the "Formatted value" in the settings of your image field.

As a general tip, if you spend a little time getting to know the Tracy Debugger module you will find it very valuable for investigating these kinds of issues.

https://processwire.com/blog/posts/introducing-tracy-debugger/
http://modules.processwire.com/modules/tracy-debugger/

 

  • Like 3
Posted

I have done it! The error is that I was naming the property images, instead of the name of the image field itself, ie. poster_image. So now I have something like:

foreach ($events as $event) {
	echo "<img src='$event->poster_image->url()'>";
}

I don't need the first() method because I have limited the field to only 1 image.

@Robin S, I was repling when you replied :P

  • 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
×
×
  • Create New...