I have some code that uses a posts featured image as the background of a DIV...
<?php if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
endif; ?>
<div class="news-image" style="background-image: url(<?php echo $image[0]; ?>); background-repeat: no-repeat; background-color: #000; background-position: center center; background-size: cover;">
Pretty simple, but I need to set a default image on this background for any posts that do NOT have a featured image set.
Is that possible by modifying my above code?
For example...
If post has featured image - show it.
If post does not have featured image - show default.jpg.
Sure you can do that, I did some rought snippet, hope you can get the info from my comments :)
<?php
// first you have to define and save your default image somewhere,, save it in options table for ex.. like this:
add_option( 'my_default_pic', 'http://www.website.com/image/jpg', '', 'yes' );
// then on page or template use like this
if (has_post_thumbnail( $post->ID ) ){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' )[0];
} else {
$image = get_option( 'my_default_pic' );
}
?>
<div class="news-image" style="#000 no-repeat center center background: url(<?php echo $image; ?>);">
body = lorem ipsum
</div>
that's it :)
hth,
edit: inserted example image path