n00dles101

Images

Recommended Posts

Hi,

Quick one I hope.

having a hard time understanding how Images work.

How do I return the first image in the images array ?

rgds, Mike.

Share this post


Link to post
Share on other sites

Mike,

For this example, I'll assume your field is called "images". To get the first image, you would do this:

$firstImage = $page->images->first();

To get a numbered index, you would do:

$nthImage = $page->images->eq($n); // where $n is a 0-based index

Or, of course you can iterate the images too:

$n = 0; 
foreach($page->images as $image) {
    echo "Image $n - " . $image->url; 
    $n++;
}
  • Like 2

Share this post


Link to post
Share on other sites

Also see: /wire/core/Array.php

There are lots of different methods in there, most based on jQuery traversal methods. This class is the basis for almost all ProcessWire arrays, including images. I use it for reference all the time. :) But I am working on getting an online reference for this class and others.

Share this post


Link to post
Share on other sites

wow.. that was quick..  :)

knew it was going to be something simple... and you seem to have covered every base here...

Thanks for your help..

Mike.

Share this post


Link to post
Share on other sites
1 hour ago, modifiedcontent said:

Is there a way to get the last image? I thought last() worked, but it seems it doesn't.

That should work just fine:

image.png.536b010b01d6ea5d82e4fd99eeefa8bc.png

  • Thanks 1

Share this post


Link to post
Share on other sites
15 minutes ago, adrian said:

That should work just fine:

image.png.536b010b01d6ea5d82e4fd99eeefa8bc.png

Hey @adrian, what is that Console window? looks really usefull

  • Like 1

Share this post


Link to post
Share on other sites
Just now, cstevensjr said:

Great work!

Thanks 🙂

It's a decent start, but I am kinda hoping for some contributions from the community for improving it.

It's all available in the docs folder (markdown files) in the repo and anyone can edit and submit PRs. I think a nice Tips & Tricks section would be beneficial, as would improvements to the rest of the docs. 

 

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites
18 hours ago, adrian said:

That should work just fine: ...

My last image was a cropped version of the first, saved as copy. I only had two images. When I duplicated the cropped image, making it the last of three images, last() did pick it up.

Variations of an image don't count for first/last etc.?

I'll keep testing if this consistently happens or if I just had a cache issue or something else.

Share this post


Link to post
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.