php - Wordpress featured image as background with default image -


i have code uses posts featured image background of 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, need set default image on background posts not have featured image set.

is possible modifying above code?

for example...

if post has featured image - show it.

if post not have featured image - show default.jpg.

sure can that, did rought snippet, hope can info comments :)

<?php   // first have define , save default image somewhere,, save in options table ex.. this:  add_option( 'my_default_pic', 'http://www.website.com/image/jpg', '', 'yes' );   // on page or template use 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 :)

hth,

edit: inserted example image path


Comments