If the image field is empty how do I get it to show nothing

219 Views Asked by At

I have the following image field code but if there's nothing there then the alt stage and src= parts still show, how can I get rid of them if the field is empty?

  echo '<div class="tab-inline">';
        echo '<img src="' . esc_url( $tension_knit ) . '" alt="tension knit icon" />';
  echo '</div>';
1

There are 1 best solutions below

0
Matt Bedford On BEST ANSWER

you'll want an if statement for that (as mentioned in comments) and if it were me I'd do something like:

  echo '<div class="tab-inline">';
    if(isset($tension_knit) && $tension_knit != "") {
        echo '<img src="' . esc_url( $tension_knit ) . '" alt="tension knit icon" />';
}
  echo '</div>';

That's going to produce an empty div for you if the $tension_knit isn't there, you'll have to decide if that's what you want or not. Your question asked specifically about getting rid of the image/alt. If you didn't want the div at all in case of empty $tension_knit, then you could just move the "if" up a line and the closing tag down a line. Good luck!