Jump to content

Recommended Posts

Posted

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?  ???

Posted

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>';
 
Posted

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.

Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...