Jump to content

Acces picture 1 to 4 of an imagefield


justnew77
 Share

Recommended Posts

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

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

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?

  • Like 1
Link to comment
Share on other sites

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

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>"
} 
  • Like 2
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...