davo Posted June 2, 2014 Posted June 2, 2014 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?
adrian Posted June 2, 2014 Posted June 2, 2014 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. 1
davo Posted June 2, 2014 Author Posted June 2, 2014 That sounds ideal. How do I access that property from the api please? I'm having trouble accessing other parts of the pw site to research ATM.
adrian Posted June 2, 2014 Posted June 2, 2014 I think you are referring to the tags property? $page->images->first()->tags or $page->images->eq(n)->tags where n is the number of the image in the images field starting with 0
davo Posted June 2, 2014 Author Posted June 2, 2014 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'.
onjegolders Posted June 2, 2014 Posted June 2, 2014 I'm guessing "tags" here is something you'd need to be testing for. $randomimg = $randogal->images->find("tags=fave")->first(); Haven't actually used them recently so not 100% sure. 1
davo Posted June 2, 2014 Author Posted June 2, 2014 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?
adrian Posted June 3, 2014 Posted June 3, 2014 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); } 1
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