Jump to content

Images


n00dles101
 Share

Recommended Posts

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

Link to comment
Share on other sites

  • 7 years later...
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
Link to comment
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.

Link to comment
Share on other sites

@modifiedcontent Maybe this is similar to 

and this issue https://github.com/processwire/processwire-issues/issues/650 which was recently fixed at https://github.com/processwire/processwire/commit/a1676b0adb3bb7877d0602e1ab1929fd5aff73d4

You might need to download and update Processwire to the latest dev commit?

Hope that helps

 

 

  • Like 2
  • Thanks 1
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...