justnew77 Posted January 18, 2014 Share Posted January 18, 2014 Hi, I got an image field named: imageK1. How can I call e.g. the second image? What I want do do: I want to list the first 4 images of the field. Cheers Link to comment Share on other sites More sharing options...
BFD Calendar Posted January 18, 2014 Share Posted January 18, 2014 It took me a while too: $image = $page->imageK1->first(2); Link to comment Share on other sites More sharing options...
adrian Posted January 18, 2014 Share Posted January 18, 2014 There are a few ways you could do this, but this approach is pretty straight-forward: $i=0; foreach($page->imageK1 as $image){ if($i==4) break; echo $image->url; } Or if you do want to specify the second image, you are looking for ->eq(1), remembering that numbering starts from 0, so 1 is actually the second image. EDIT: Sorry, see corrected code below: http://processwire.com/talk/topic/5359-acces-picture-1-to-4-of-an-imagefield/?p=51662 Link to comment Share on other sites More sharing options...
justnew77 Posted January 18, 2014 Author Share Posted January 18, 2014 Both thing do not work: $i=0; foreach($page->imageK1 as $imageK1){ if($i==4) break; echo "<a class='fancybox' rel='gallery1' href='$imageK1->url'>"; echo "<img src='$imageK1->url' height='150'></a>"; } the code above put out ALL images. and echo $image = $page->imageK1->first(2); echo $image = $page->imageK1->first(3); echo $image = $page->imageK1->first(4); echo $image = $page->imageK1->first(5); put out only the filename of the FIRST image, 4 times! Link to comment Share on other sites More sharing options...
Soma Posted January 18, 2014 Share Posted January 18, 2014 $images = $page->images->slice(0,3); Link to comment Share on other sites More sharing options...
BFD Calendar Posted January 18, 2014 Share Posted January 18, 2014 And, you have to put it into html to show the image instead of the filename <img src='{$page->imageK1->first(2)->url}' /> (or adapt Soma's solution to this). Link to comment Share on other sites More sharing options...
Soma Posted January 18, 2014 Share Posted January 18, 2014 There is no argument to first() its always the first. 1 Link to comment Share on other sites More sharing options...
BFD Calendar Posted January 18, 2014 Share Posted January 18, 2014 I'm obviously a beginner.... Link to comment Share on other sites More sharing options...
justnew77 Posted January 18, 2014 Author Share Posted January 18, 2014 echo $images = $page->images->slice(0,3); get me an Error: Call to a member function slice() on a non-object Link to comment Share on other sites More sharing options...
adrian Posted January 18, 2014 Share Posted January 18, 2014 If you're using Soma's slice approach, you need to use your image field: imageK1 echo $images = $page->imageK1->slice(0,3); Did you try $page->imageK1->eq(1)->url; rather than those first(x) which won't work because of what Soma mentioned? 1 Link to comment Share on other sites More sharing options...
adrian Posted January 18, 2014 Share Posted January 18, 2014 PS There was a slight omission in my original code. I missed the counter to up the value of $i on each loop. This should work just fine to echo out the first four images $i=0; foreach($page->imageK1 as $image){ if($i==4) break; echo "<a class='fancybox' rel='gallery1' href='$image->url'>"; echo "<img src='$image->url' height='150'></a>" $i++; } Link to comment Share on other sites More sharing options...
adrian Posted January 18, 2014 Share Posted January 18, 2014 I thought I should also clarify that if you use Soma' approach, you will still need to iterate through the $images array like this: $images = $page->imageK1->slice(0,3); foreach($images as $image){ echo "<a class='fancybox' rel='gallery1' href='$image->url'>"; echo "<img src='$image->url' height='150'></a>" } 2 Link to comment Share on other sites More sharing options...
justnew77 Posted January 20, 2014 Author Share Posted January 20, 2014 Thank you all for your help Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now