Jump to content

Displaying image on page


Recommended Posts

I have a template with an "images" field. I'm trying to display it on the page, I'm using these lines, with results I do not understand.

$page->images // teddy.jpg
$page->images->httpUrl // void
$page->images->url // /site/assets/files/1085/
$page->images[0]->url // error: accessing non-object

I thought `$page->images->url` would work, but no.

Why is it?

Link to comment
Share on other sites

It depend on how you configured the field. You can make it work by calling $page->images->first()->url 


The doc say:


On the details tab, you may set the maximum quantity of images the field may contain. Please note that if you set it to one (1), the field will usually behave as a single image rather than an array of images, and this affects how you access it from the API. 

For more informations: https://processwire.com/docs/fields/images/

  • Like 1
Link to comment
Share on other sites


like @flydev ?? says, it depends on how you've set your field options
what i can add is that images[0] won't work, first() or ->eq(0) are far better but, even if it seems obvious i'd just like to add something else, if you want to display images you have to use the img tag... (sorry again if it is that ovious) so, something like this will work assuming your field is set to contain an array

<?php foreach ($page->images as $img): ?>
<img src="<?php echo $img->url; ?>" alt="" />
<?php endforeach; ?>

if it is set as a single file field

<img src="<?php echo $page->images->url; ?>" alt="" />

will do the trick ?

in case it helps

have a nice day

Link to comment
Share on other sites

13 hours ago, pwired said:

images is an array .... so how would $page->images->url work ?

as i said, if images is set to be a single-file field read the full answer you'll see ?

and it's something i often use for some fields, say the top page image for example, using the single-file option pw still allows you to fill the image field with as many images you want but (if your field is named images...) $page->images-
>url will always take the first one, simple way to give a customer a "stock" and a possible choice that can change when you want just ordering the files to put the one you want to see in pole position

have a nice reading the full posts day ?

Link to comment
Share on other sites

Yes I understand what you are saying there but setting images to be a single-file field is only a pre-option that you can choose to set before starting to use images. Why not just leave images as an array and use the api and/or php to do whatever you need from it to get whatever you want from it.

Link to comment
Share on other sites

Hi @pwired

well, very often on a website page you will end with let's say, a top page image (or carrousel) image that can be used in the content (text and so on), just for fun add a repeater with blocks linking to another page...
pw allows you to build an intuiitive back end interface with
- a single-file image field for the top image with a specific description about the format/ratio/min-width
- same thing in case of a top carrousel but with an field set to an array
in these two cases you can set image crop ratio plugin with the needed ratio
- les add in each repeater item a single file image field with a 1:1 ratio beacause your design requires a square
of course
- why not another array like uimage field for a gallery that will appera on the page only if there are images in
... i let you imagine the other possibilities

doing this makes your back end easy to use and understand for customers that stop being afraid of feeding their website, exactly what i love and so do my customers ? with pw
the more granular your back end is the happier your users are too ? and of course field being in the most logical order according the the template design, thanks to pw incredible flexibility and variety of chosable fields and options, it makes quite complex designs so easy to edit...

have a nice day ?


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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...