1
votes

How to display a placeholder image if there's no image set up in WordPress? The picture is displayed by the $property_image var. It is a featured image from a custom post (not a WordPress original one).

It is defined by the code below, displays the featured image of the post if set up, if not, only shows the alt tag content ("Upcoming picture..."):

$property_image = dreamvilla_mp_get_device_image($property_ID);

Below my code from a WordPress theme:

$html .= '<div class="property-list-list property-listing-list-full">
        <div class="col-xs-12 col-sm-4 col-md-4 property-list-list-image">
            <a href=' . esc_url(get_permalink($property_ID)) . '>
                <img '. $property_image . ' alt="Upcoming picture..." class="img-responsive">
            </a>

            ' . $featured_proeprty_label . '
            ' . $featured_proeprty_label_icon . '
            ' . dreamvilla_mp_agent_favorites_property_icon($property_ID) . '
        </div>
2
what contain $property_image variable when there is image. and what its contain, when there isnt image? - Samvel Aleqsanyan
Hi @SamvelAleqsanyan I've added some content to my question - thmsgrmd

2 Answers

2
votes

I'd place this:

if( empty( $property_image ) ) {
    $property_image = 'src="your_fallback_image"';
}

directly after:

$property_image = dreamvilla_mp_get_device_image($property_ID);

What to be used to determine if the variable is empty depends on what is stored in the variable.

0
votes

Directly before the img tag, you could insert this:

<?php
  if($property_image == '') { 
    $property_image ="scr='http://placehold.it/300x200/f0a'"; 
  }
?>

It checks whether that variable is empty and if yes, puts a links to a placeholder image into it, including the src attribute.