BitPoet Posted August 25, 2016 Share Posted August 25, 2016 Does this: $sub_image = $pages->get("has_parent.id=$sub->id,template=product, sort=random")->product_images->find('tags=prod')->getRandom(); work? (You may want to also include a "product_images.tags=prod" subfield selector for the pages in case you have pages without a production ready image) 1 Link to comment Share on other sites More sharing options...
Macrura Posted August 26, 2016 Share Posted August 26, 2016 18 hours ago, opalepatrick said: $sub_image = $pages->get("has_parent.id=$sub->id,template=product, sort=random")->product_images->getTag('prod')->getRandom(); one problem with this is that i think you wanted to use findTag('prod')->getRandom() (?) Link to comment Share on other sites More sharing options...
opalepatrick Posted August 29, 2016 Share Posted August 29, 2016 Yes I did Macrura... I want to pick a random page in a category, then pick a random image from the product_images field of that random page that is tagged with 'prod' Link to comment Share on other sites More sharing options...
Macrura Posted August 29, 2016 Share Posted August 29, 2016 ok b/c you used getTag which only gets the first, have you tried findTag yet (returns WireArray) Link to comment Share on other sites More sharing options...
opalepatrick Posted August 29, 2016 Share Posted August 29, 2016 $sub_image = $pages->get("has_parent.id=$sub->id,template=product, sort=random")->product_images->findTag('prod')->getRandom(); That returned a non-object error when I tried to access the variable next line, Macrura? Link to comment Share on other sites More sharing options...
Macrura Posted August 29, 2016 Share Posted August 29, 2016 it should be functionally equivalent to product_images->find('tags=prod') would need to see full code you are using to assist further, as well as the exact error message; this might also be helpful: https://processwire.com/api/ref/pagefiles/find-tag/ Link to comment Share on other sites More sharing options...
Robin S Posted August 29, 2016 Share Posted August 29, 2016 6 hours ago, opalepatrick said: That returned a non-object error when I tried to access the variable next line I think the problem is something @BitPoet mentioned above: you want to be sure the matched page has some images tagged "prod" before you try and get one of them. As @Macrura says, find('tags=prod') and findTag('prod') will do the same thing. // either... $sub_image = $pages->get("has_parent.id=$sub->id, template=product, product_images.tags=prod, sort=random")->product_images->find('tags=prod')->getRandom(); // or... $sub_image = $pages->get("has_parent.id=$sub->id, template=product, product_images.tags=prod, sort=random")->product_images->findTag('prod')->getRandom(); // then... if($sub_image) { // whatever... } Edit: If it's possible that no pages have any images tagged "prod" (and it's probably a good idea in any case) then you should actually break this into two steps to make sure your $pages->get() operation has returned a page. $sub_image_page = $pages->get("has_parent.id=$sub->id, template=product, product_images.tags=prod, sort=random"); if($sub_image_page->id) { $sub_image = $sub_image_page->product_images->find('tags=prod')->getRandom(); } if($sub_image) { // whatever... } 1 Link to comment Share on other sites More sharing options...
opalepatrick Posted August 30, 2016 Share Posted August 30, 2016 Thanks Robin S, Macrura and BitPoet... Breaking it down as per last edit was the solution... $sub_image_page = $pages->get("has_parent.id=$sub->id, template=product, product_images.tags=prod, sort=random"); if($sub_image_page->id) { $sub_image = $sub_image_page->product_images->find('tags=prod')->getRandom(); if($sub_image) { echo "<img src='<?=$sub_image->width(300)->url?>' />"; } else { echo "<p>No prod tagged images</p>"; } } else { echo "<p>no pages</p>"; } 1 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