Joanna Mikalai Joanna Mikalai -3 years ago 82
PHP Question

If Statement Inside Html

I am trying to use an IF statement if variable

$credit
is not empty. However, I get a parse error. Could anyone please help with my syntax?

$credit = get_field('fl_credit', $image['id']);
return '<div class="kalim"><img title="' .
esc_attr( sprintf( the_title_attribute( 'echo=0' ) ) ) .
'" alt="' . esc_attr( sprintf( the_title_attribute( 'echo=0' ) ) ) .
'" src="' . $image['url'] . '" /><div class="kalca">' .
$image['caption'] . '</div>' .
if($credit) {'<div class="kalcr">Credit:' . $credit . '</div></div>' };

Answer Source

Without knowing the error, I suggest do a short if, so:

 $credit = get_field('fl_credit', $image['id']);
   return '<div class="kalim"><img title="' .
      esc_attr( sprintf( the_title_attribute( 'echo=0' ) ) ) . 
     '" alt="' . esc_attr( sprintf( the_title_attribute( 'echo=0' ) ) ) .
     '" src="' . $image['url'] . '" /><div class="kalca">' . 
     $image['caption'] . '</div>' . 
     (!empty($credit) ? '<div class="kalcr">Credit:' . $credit . '</div></div>': '' ) ;

You can see more here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download