特征图像If Else条件

时间:2013-08-30 作者:Jeremi Liwanag

我将我的横幅背景设置为特色图片的URL。

<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>
如果我没有放置特色图像,我希望设置默认的横幅背景。怎么能做到呢?

1 个回复
最合适的回答,由SO网友:iEmanuele 整理而成

将代码更改为:

<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

诀窍在于:

// 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;
希望有帮助!

结束

相关推荐

Theme Custom Pages

我在做一个函数。新主题系统的php文件。我需要知道如何通过URL使自定义模板可执行。所以如果有人去http://website.com/my-custom-template/ 如何使加载的页面从模板中读取my-custom-template.php 在主题文件夹中?