Jump to content

Extra property to image field


davo
 Share

Recommended Posts

I'm creating a site that has a huge image gallery. I'm using the api to pull out randomised images from the gallery and display them on the home page. However, the client doesn't like all the images that are I'm the gallery. So I was wondering if there was a way could add an extra field property the the image to mark is as a favourite. Is this possible?

Link to comment
Share on other sites

It is possible to extend the images field with extra fields:

https://processwire.com/talk/topic/417-extending-image-field/?p=3351

but the simplest option for you might be to enable tags and write "fav" or something like that in there. Details tab > Use Tags?

Or you could also have one image per page and then you have complete flexibility in adding custom fields for each image.

  • Like 1
Link to comment
Share on other sites

First day back today and i'm not as quick as desired with the api, I've tried this:

$randogals =$pages->find("template=gallery-album");
$randogal = $randogals->getRandom();
$randomimg = $randogal->images->first()->tags->size($thumbWidth, $thumbHeight);

but that produced me a 500 error.

What i'm trying to get is a random gallery with a random image where in each instance the tag contains the text 'fav'.

Link to comment
Share on other sites

got there at last like this:

$randogals =$pages->find("images.tags=fav");
$randogal = $randogals->getRandom();
$randomimgarray = $randogal->images->findTag('fav');
$randomimg = $randomimgarray->getRandom()->size($thumbWidth, $thumbHeight);

$randogal2 = $randogals->getRandom();
$randomimgarray2 = $randogal2->images->findTag('fav');
$randomimg2 = $randomimgarray2->getRandom()->size($thumbWidth, $thumbHeight);

I actually display two favourite images on the same page. I wonder if anyone can think of a simple tidy way to ensure that the same image isn't used twice on the same page at the same time?

Link to comment
Share on other sites

You can do it in one with findRandom(2) and then you won't need to worry about duplicate images:

$randogals =$pages->find("images.tags=fav");
$randogal = $randogals->getRandom();
$randomimgarray = $randogal->images->findTag('fav');
foreach($randomimgarray->findRandom(2) as $randomimage){
    echo $randomimage->size($thumbWidth, $thumbHeight); 
}
  • Like 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...