Wordpress的特色图像作为默认图像的背景


Wordpress featured image as background with default image

我有一些代码,使用一个帖子的特色图像作为div的背景…

 <?php if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); 
endif; ?>
<div class="news-image" style="background-image: url(<?php echo $image[0]; ?>); background-repeat: no-repeat;  background-color: #000; background-position: center center; background-size: cover;">

很简单,但是我需要在这个背景上为任何没有特色图像集的帖子设置一个默认图像。

是可能的修改我上面的代码?

例如…

如果文章有特色图片-显示它

如果post中没有feature image - show default.jpg.

当然你可以这样做,我做了一些粗略的片段,希望你能从我的评论中得到信息:)

<?php 
// first you have to define and save your default image somewhere,, save it in options table for ex.. like this:
add_option( 'my_default_pic', 'http://www.website.com/image/jpg', '', 'yes' );

// then on page or template use like this
if (has_post_thumbnail( $post->ID ) ){
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' )[0]; 
} else {
    $image = get_option( 'my_default_pic' );
}
?>
<div class="news-image" style="#000 no-repeat center center background: url(<?php echo $image; ?>);"> 
    body = lorem ipsum
</div>

就这样了:)

hth,