0

I'm using the following code to set my post featured image as a background-image

<?php if (has_post_thumbnail( $post->ID ) ):

 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); 

endif; ?>

How can I add an else statement so if the post doesn't have a featured image it will use a specific image /images/featured.jpg for example.

closed as off-topic by Rarst Jan 9 '14 at 17:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – Rarst
If this question can be reworded to fit the rules in the help center, please edit the question.

1

You can just use else below your code.

<?php 
if (has_post_thumbnail( $post->ID ) ):
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); 
else:
    $image = array( '/images/featured.jpg' ); 
endif;

$image_url = $image[0];
?>
0
<?php if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); 
endif; ?>

<div style="background:url(<?php echo $image[0]; ?>)">
<p>teing for background image</p>
<p>teing for background image</p>
<p>teing for background image</p>
<p>teing for background image</p>
<p>teing for background image</p>
</div>

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.