Tyssen Posted March 6, 2013 Share Posted March 6, 2013 Originally I had: $outMain .= '<ul class="nb staff">'; foreach($page->children as $staff) : $outMain .= '<li> <h2 class="h3">'.$staff->title.'</h2> <p class="location">'.$staff->position.'</p>' .$staff->body.' </li>'; endforeach; $outMain .= '</ul>'; and that was working fine, but when I add in some code to show images if they're present: $outMain .= '<ul class="nb staff">'; foreach($page->children as $staff) : $image = $staff->images->first(); if(!$image) continue; $thumb = $image->size(140, 190); $outMain .= '<li> <h2 class="h3">'; if($thumb) $outMain .= '<img src="'.$thumb->url.'" alt="" class="img-r bdr">'; $outMain .= $staff->title.'</h2> <p class="location">'.$staff->position.'</p>' .$staff->body.' </li>'; endforeach; $outMain .= '</ul>'; now, only the pages/entries with images are shown. If I comment out the image stuff, all the entries display. What am I doing wrong? Link to comment Share on other sites More sharing options...
fmgujju Posted March 6, 2013 Share Posted March 6, 2013 The problem is at the first IF condition where if there is no image then you're continue to loop which will skip rest of the code in the loop and look for next staff. if(!$image) continue; Here is the updated code: (not tested) $outMain .= '<ul class="nb staff">'; foreach($page->children as $staff) : $outMain .= '<li> <h2 class="h3">'; $image = $staff->images->first(); if($image) $thumb = $image->size(140, 190); if($thumb) $outMain .= '<img src="'.$thumb->url.'" alt="" class="img-r bdr">'; $outMain .= $staff->title.'</h2> <p class="location">'.$staff->position.'</p>' .$staff->body.' </li>'; endforeach; $outMain .= '</ul>'; Link to comment Share on other sites More sharing options...
Tyssen Posted March 6, 2013 Author Share Posted March 6, 2013 Yeah, Ryan pointed that out to me on Twitter but with that in place, what's happening is that entries that don't have images pick up the image from the previous entry that does have an image. Link to comment Share on other sites More sharing options...
adrian Posted March 6, 2013 Share Posted March 6, 2013 You could probably just add $image = ''; as the first thing inside the foreach so it gets emptied. You might need to do the same with $thumb Link to comment Share on other sites More sharing options...
Tyssen Posted March 6, 2013 Author Share Posted March 6, 2013 Yep, that's what I ended up doing. 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