Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have my banner background set to the URL of the featured image.

<div id='xpro_shell_text' >
  <?php if (has_post_thumbnail( $post->ID ) ): ?>
  <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
  <div id="xpro_shell_content_header"  class='content_about_us_header white' style="background:url('<?php echo $image[0]; ?>') no-repeat;">
    <div id='xpro_shell_content_header_blurb'>
      <h1 class='greenHeader cufon_reg'>
        <?php the_title(); ?>
      </h1>
      <p>
        <?php $message=get_post_meta($post->ID, "myBlurb", true);
 if (get_post_meta($post->ID, "myBlurb", true)) {
 echo ($message);
 } ?>
      </p>
    </div>
  </div>
  <?php endif; ?>

  <!--end xpro_shell_content_wrapper_top --> 
</div>

I want a default banner background set if i didn't put a Featured image. How can do that?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Change your code to:

<div id='xpro_shell_text' >
  <?php 
    if ( has_post_thumbnail( $post->ID ) ) :
        $imageInfo = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
        $imageUrl = $imageInfo[0];
    else:
        $imageUrl = get_template_directory() . 'PATH TO YOUR DEFAULT IMAGE';
    endif;
  ?>
  <div id="xpro_shell_content_header"  class='content_about_us_header white' style="background:url('<?php echo $imageUrl; ?>') no-repeat;">
    <div id='xpro_shell_content_header_blurb'>
      <h1 class='greenHeader cufon_reg'>
        <?php the_title(); ?>
      </h1>
      <p>
        <?php 
             $message=get_post_meta($post->ID, "myBlurb", true);
             if ( get_post_meta($post->ID, "myBlurb", true) ) {
                echo ($message);
             } 
        ?>
      </p>
    </div>
  </div>

  <!--end xpro_shell_content_wrapper_top --> 
</div>

UPDATE

The trick is here:

// If we have a featured image, it will be used as background image using the same logic you already used, but we put the src value in a specific variable: $imageUrl
if ( has_post_thumbnail( $post->ID ) ) :
  $imageInfo = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
  $imageUrl = $imageInfo[0];
// if not, we define $imageUrl with our default image src value
else:
  $imageUrl = get_template_directory() . 'PATH TO YOUR DEFAULT IMAGE';
endif;

Hope it helps!

share|improve this answer
    
please give more explanation –  JMau Aug 30 '13 at 11:14
    
changed my vote. Thanks. –  JMau Aug 30 '13 at 11:21
    
@JMau sorry, for the inconvenience but i've pasted over a previously answer from my text editor, overwriting my explanation –  iEmanuele Aug 30 '13 at 11:21
    
@JMau, no problem : ) –  iEmanuele Aug 30 '13 at 11:22
    
thanks this worked. i just did minor edits to it –  Jeremi Liwanag Aug 30 '13 at 11:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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