0

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?

2

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!

  • please give more explanation – JMau Aug 30 '13 at 11:14
  • @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
  • @JeremiLiwanag, you're welcome – iEmanuele Aug 30 '13 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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