2015-07-09 156 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