Peter Knight Posted October 24, 2014 Share Posted October 24, 2014 I'm trying to output a series of photographs onto a index page. Each photo is from a different page so I'm using the $pages API. Everything is working bar the image and link to the main Graduate detail page. My PHP call <?php $graduates = $pages->find("template=graduate-detail, sort=-date"); foreach ($graduates as $graduate) echo " <img src='$graduate->graduate_photo->url' /> <h3>{$graduate->title}</h3> {$graduate->graduate_summary}<br/> <a href='{$graduate->url}'>Read more</a> <hr/> " ;?> What's happening Instead of outputting a JPG from a field called graduate_photo, i'm left with the following: <img src="graduate-hazel-scully.jpg->url"> As you can see, the image name is correct but the path is incomplete. It's outputting the '->url' too for some reason. I've red thorugh the PW images documentation so I'm not sure what my issue is. Possiblt it's because the docs are mostly giving examples where an image is called from the $page instead of $pages. Link to comment Share on other sites More sharing options...
Philipp Posted October 24, 2014 Share Posted October 24, 2014 Didn't you missed {} around the PHP call for the image URL? 1 Link to comment Share on other sites More sharing options...
Peter Knight Posted October 24, 2014 Author Share Posted October 24, 2014 Didn't you missed {} around the PHP call for the image URL? I did but it didn't fix anything. Actually, it outputs the complete path to my image except the actual image name <img src="/my-pretend-site.com/site/assets/files/1093/"> Link to comment Share on other sites More sharing options...
adrian Posted October 24, 2014 Share Posted October 24, 2014 Looks like you have a multi-image field without using ->first() 1 Link to comment Share on other sites More sharing options...
Peter Knight Posted October 24, 2014 Author Share Posted October 24, 2014 Definitely need to go back to PHP junior school Do I need to create a separate variable for the first image and then call that in my echo statement <?php $graduates = $pages->find("template=graduate-detail, sort=-date"); $myfirstimage = $pages->graduate_photo->first(); foreach ($graduates as $graduate) echo " <img src='{$myfirstimage->url}' /> <h3>{$graduate->title}</h3> {$graduate->graduate_summary}<br/> <a href='{$graduate->url}'>Read more</a> <hr/> " ;?> Above doesn't work but I think Im on the right track Link to comment Share on other sites More sharing options...
adrian Posted October 24, 2014 Share Posted October 24, 2014 <?php $graduates = $pages->find("template=graduate-detail, sort=-date"); foreach ($graduates as $graduate) { echo " <img src='{$graduate->graduate_photo->first()->url}' /> <h3>{$graduate->title}</h3> {$graduate->graduate_summary}<br/> <a href='{$graduate->url}'>Read more</a> <hr/> "; } ?> 3 Link to comment Share on other sites More sharing options...
Peter Knight Posted October 24, 2014 Author Share Posted October 24, 2014 Adrian - thanks. I've only spent 5 hours trying to solve this and a couple of hours on the forums. Link to comment Share on other sites More sharing options...
SiNNuT Posted October 24, 2014 Share Posted October 24, 2014 Adrian - thanks. I've only spent 5 hours trying to solve this and a couple of hours on the forums. Hitting these kinds of roadblocks makes you better in the long run. At some point you'll never forget that when file or image fields are set to allow more then 1 file/image, you'll have to iterate over them because they are a WireArray. Or use a method like adrian showed. Also, this concept is explained at the start of the images documentation you referred to. One of the most common sources of confusion here on the forums . 4 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted October 24, 2014 Share Posted October 24, 2014 Same as SiNNut said: People tend to learn more from failure then from success... regard this as a success story Link to comment Share on other sites More sharing options...
Soma Posted October 24, 2014 Share Posted October 24, 2014 If we pay 5$ every time there someone was/has this issue we could buy a skyscraper. Or take all the time spent with this particular PW problem, we could free off the rest of the year. ; p 1 Link to comment Share on other sites More sharing options...
Peter Knight Posted October 25, 2014 Author Share Posted October 25, 2014 @SiNNuT Image fields can hold multiple images (a WireArray) so I need to specify which exactly which image I want to call. Got it. Getting the tatoo on my face next week 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted October 25, 2014 Share Posted October 25, 2014 New avatar next week ? 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