2013-10-01 26 views
1

我在wordpress的Function.PHP文件中插入了此代碼。它基本上做的是:在PHP腳本中爲IF添加一個ELSE以獲取默認圖像

當用戶點擊pinterest按鈕時,它會忽略博客文章頁面中的所有圖像,而是選擇/返回要固定的特徵圖像。

function catch_that_image($size = 'full') { 
    global $post; 
    if (has_post_thumbnail($post->ID)) { 
     $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $size); 
     return $featured_image[0]; 
    } 
    return false; 
} 

我有上面的代碼沒有問題,但後來我意識到,什麼,如果沒有特色圖片

是否有可能,如果有人可以修改,以添加以下if和else條件代碼:

IF特色圖片是至今:

運行上面的腳本。 (我認爲這是蓋在上面的代碼和正常工作)

要是沒有特色圖片,選擇/返回默認圖像被固定。

我不太清楚如何將此代碼(以下)集成到上面的代碼。對不起,但我缺乏這方面的知識。

if(empty($first_img)){ //Defines a default image 
    $first_img = "http://www.mywebsite/wp-content/themes/Default_Image.jpg"; 
    } 

感謝您verymuch

回答

2

使用下面的代碼:

function catch_that_image($size = 'full') { 
    global $post; 
    if (has_post_thumbnail($post->ID)) { 
     $featured_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $size); 
     if(empty($featured_image[0])){ //Defines a default image 
     $featured_image[0] = "http://www.mywebsite/wp-content/themes/Default_Image.jpg"; 
     } 
     return $featured_image[0]; 
    } 
    return false; 
}