2015-07-09 157 views
0

我有一个使用功能的图像作为一个DIV的背景帖子一些代码...WordPress的特色图片作为背景的默认图像

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

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

修改我的上面的代码可以吗?

例如...

如果发布有特色图像 - 显示它。

如果帖子中没有特色图片 - show default.jpg。

回答

0

当然,你可以做到这一点,我做了一些rought片断,希望你能从我的意见:)

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

就是这样:)

心连心,

编辑得到的信息:插入示例图像路径

+0

显示特色图像但不是默认值。你可以向你的代码添加一个真实的世界链接,以显示默认图像的位置?如果图片是http://website.com/image/jpg这是如何适合你的代码? – lowercase

+0

我在第一行的'add_option'中有硬编码的URL ..希望能帮到你 –

+0

不幸的是,仍然没有显示没有设置特征图像的帖子的默认图像。该代码没有在URL字段中输入任何内容,如下所示:style =“background-image:url();:URL参数为空 – lowercase