设置为背景图像的默认特色图像

时间:2014-01-05 作者:Steven_Harris_

我正在使用以下代码将我的帖子特色图片设置为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; ?>
如何添加else语句,以便在帖子没有特色图片时使用特定图片/images/featured.jpg 例如

2 个回复
SO网友:Shazzad

您可以只使用代码下面的其他内容。

<?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];
?>

SO网友:Ravi Patel
<?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

结束

相关推荐

Get 1 more post in loop

例如,在简单循环中:$loop = new wp_query(array(\'posts_per_page\' => 3)); while($loop->have_posts()) : $loop->the_post(); if(get_post_meta($post->ID, \'skip_me\', true) == true){ // do something to ask for one more post, &#x