taotoo Posted July 25, 2022 Share Posted July 25, 2022 I want to display some images. Sometimes the images will be from the current page's image field. But sometimes they will instead be from the image fields of multiple items in a page reference field. I presume in the first case I'm getting an array of images, but for the second I'm getting something else (junk maybe)? // Get images of the author if they exist if $page->images !='': $source = $page->images; // Otherwise get images of their book covers else: $source = $page->page_ref_for_their_books->images->first; endif; foreach $source as $source: echo $source->size(100,100)->url; endforeach; Link to comment Share on other sites More sharing options...
3fingers Posted July 25, 2022 Share Posted July 25, 2022 Looking at your pseudo-code you're outputting a single image (the first one) from the page reference field, so you don't need to loop through it. // Get images of the author if they exist if $page->images !='': foreach $page->images as $source: echo $source->size(100,100)->url; endforeach; // Otherwise get images of their book covers else: $source = $page->page_ref_for_their_books->images->first; echo $source->size(100,100)->url; endif; 1 Link to comment Share on other sites More sharing options...
taotoo Posted July 25, 2022 Author Share Posted July 25, 2022 34 minutes ago, 3fingers said: Looking at your pseudo-code you're outputting a single image (the first one) from the page reference field, so you don't need to loop through it. // Get images of the author if they exist if $page->images !='': foreach $page->images as $source: echo $source->size(100,100)->url; endforeach; // Otherwise get images of their book covers else: $source = $page->page_ref_for_their_books->images->first; echo $source->size(100,100)->url; endif; The thing is there will be multiple pages in the page reference field, and I want to get the first image from each of them. Link to comment Share on other sites More sharing options...
Micthew Posted July 26, 2022 Share Posted July 26, 2022 Quote $avatar = $config->urls->templates.'assets/img/placeholder-user.png'; $imagesAvatar = $page->images; if ($imagesAvatar) { foreach ($imagesAvatar as $imageAvatar) { $avatar = $imageAvatar->size(200,200)->url; } } else { $avatar = $config->urls->templates.'assets/img/placeholder-user.png'; } This is my code... But, when i create "else {}" and the image in data is empty, placeholder-user.png is not showing, so i re-type "$config->urls->templates.'assets/img/placeholder-user.png" to $avatar on the top variable, and then when the image in data is empty, placeholder-user.png is showing. I don't know whats wrong with my code, but the code is working for me. Micthew 1 Link to comment Share on other sites More sharing options...
Gideon So Posted July 26, 2022 Share Posted July 26, 2022 Hi @Micthew It means $imagesAvatar always return a value. try replace $imagesAvatar with $page->images in the third line of your code and see if it works. Gideon 1 Link to comment Share on other sites More sharing options...
Micthew Posted July 26, 2022 Share Posted July 26, 2022 Hi @Gideon So Thank you Gideon, it's works 1 hour ago, Micthew said: if ($imagesAvatar) { Change to Quote if ($page->images) { Link to comment Share on other sites More sharing options...
taotoo Posted July 26, 2022 Author Share Posted July 26, 2022 1 hour ago, Micthew said: This is my code... But, when i create "else {}" and the image in data is empty, placeholder-user.png is not showing, so i re-type "$config->urls->templates.'assets/img/placeholder-user.png" to $avatar on the top variable, and then when the image in data is empty, placeholder-user.png is showing. I don't know whats wrong with my code, but the code is working for me. Micthew In my case both scenarios will have multiple images, and I want them to be output with the same img tag, within the same foreach. Maybe I need to change the middle section to something like this. I don't know how to do it though (a wire array maybe?): // Get images of the author if they exist if $page->images !='': $source = $page->images; // Otherwise get images of their book covers else: foreach($page->page_ref_for_their_books as $a_page): $an_image = $a_page->images->first; endforeach; $source = every $an_image merged together to mimic the result of the first condition; endif; foreach ($source as $source): echo $source->size(100,100)->url; endforeach; Link to comment Share on other sites More sharing options...
3fingers Posted July 26, 2022 Share Posted July 26, 2022 // Get images of the author if they exist if count($page->images): echo $page->images->each(function($image) { return "<li><a href='$image->size(100,100)->url'>$image->description</a></li>"; // Just an output example }); // Otherwise get images of their book covers else: foreach($page->page_ref_for_their_books as $all_pages): foreach($all_pages->images as $image): echo "<li><a href='$image->size(100,100)->url'>$image->description</a></li>"; endforeach; endforeach; endif; 1 Link to comment Share on other sites More sharing options...
taotoo Posted July 26, 2022 Author Share Posted July 26, 2022 51 minutes ago, 3fingers said: // Get images of the author if they exist if count($page->images): echo $page->images->each(function($image) { return "<li><a href='$image->size(100,100)->url'>$image->description</a></li>"; // Just an output example }); // Otherwise get images of their book covers else: foreach($page->page_ref_for_their_books as $all_pages): foreach($all_pages->images as $image): echo "<li><a href='$image->size(100,100)->url'>$image->description</a></li>"; endforeach; endforeach; endif; If possible I want to have only one section of code for the output, as in reality it's a srcset with lots going on. Though I suppose with your example I could replace the <li> with an include. Otherwise I was wondering if I could build an array, but neither of the two examples below seem to work (the changed lines indicated by asterisks): edit: I had a typo, now it seems both examples below work! // Get images of the author if they exist if $page->images !='': $source = $page->images; // Otherwise get images of their book covers else: foreach($page->page_ref_for_their_books as $a_page): $an_image = $a_page->images->first; * $source[] = $an_image; * endforeach; endif; foreach ($source as $source): echo $source->size(100,100)->url; endforeach; Or: // Get images of the author if they exist if $page->images !='': $source = $page->images; // Otherwise get images of their book covers else: foreach($page->page_ref_for_their_books as $a_page): * $source[] = $a_page->images->first; * endforeach; endif; foreach ($source as $source): echo $source->size(100,100)->url; endforeach; 1 Link to comment Share on other sites More sharing options...
taotoo Posted July 30, 2022 Author Share Posted July 30, 2022 Further to this, if I use a foreach as above, it works fine: foreach ($source as $source): echo $source->size(100,100)->url; endforeach; But if I use -> first, it doesn't work: echo $source->first->size(100,100)->url; Is this because I've made a PHP array, and it's the case that ->first only works with ProcessWire arrays? Link to comment Share on other sites More sharing options...
DaveP Posted July 31, 2022 Share Posted July 31, 2022 On 7/30/2022 at 12:02 PM, taotoo said: Is this because I've made a PHP array, and it's the case that ->first only works with ProcessWire arrays? Yep, but, PW gives you methods for working with WireArrays - https://processwire.com/api/ref/functions/wire-array/. This way you can use ->first() or any of the usual methods. You'll need to refactor your code a bit but it might well be worth it. 1 Link to comment Share on other sites More sharing options...
taotoo Posted August 1, 2022 Author Share Posted August 1, 2022 8 hours ago, DaveP said: Yep, but, PW gives you methods for working with WireArrays - https://processwire.com/api/ref/functions/wire-array/. This way you can use ->first() or any of the usual methods. You'll need to refactor your code a bit but it might well be worth it. Thank you DaveP! // Otherwise get images of their book covers else: $source = new WireArray(); foreach($page->page_ref_for_their_books as $a_page): $source->add($a_page->images->first); endforeach; endif; echo $source->first->size(100,100)->url; 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