Francis Alvin Tan
Francis Alvin Tan

Reputation: 1087

Add a "no image placeholder" on Elementor pro post element if there is no featured image

Is there a filter of some sort that can add an image if there is no featured image present when using the Elementor pro "post" element. Because the title goes up if there is no image placed and it breaks the sites display

UI with 1 image and without 2 images present on a Row showing different text alignments

I want to add a placeholder image like this one below when no featured image is available:

placeholder image

Upvotes: 0

Views: 14218

Answers (1)

kaize
kaize

Reputation: 811

You can add this filter to you theme functions.php :

function mlnc_filter_post_thumbnail_html( $image_placeholder ) {
    // If there is no post thumbnail,
    // Return a default image
    if ( '' == $image_placeholder ) {
        return '<img src="' . get_template_directory_uri() . '/images/default-thumbnail.png"/>';
    }
    // Else, return the post thumbnail
    return $image_placeholder;
}
add_filter( 'post_thumbnail_html', 'mlnc_filter_post_thumbnail_html' );

Another way is to go where the image is outputting and add an If statement like below:

<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg"/>
<?php } ?>

Upvotes: 0

Related Questions