2017-02-12 123 views
1

我有這從URL保存爲存儲圖像的功能,唯一的問題是這一切似乎很好地工作比其他圖像「無法找到」保存圖像從URL到WordPress的媒體庫

圖片和名稱都在媒體庫中,但圖片被打破,因爲當您通過url打開圖片時,它會顯示「未找到」

如果我錯過了某些東西,您能否告訴我一些問題。

function set_image_from_url($url) { 


    $tmp = download_url($url); 

    $file_array = array(
     'name' => basename($url), 
     'tmp_name' => $tmp 
    ); 

    /** 
    * Check for download errors 
    * if there are error unlink the temp file name 
    */ 
    if (is_wp_error($tmp)) { 
     @unlink($file_array[ 'tmp_name' ]); 
     return $tmp; 
    } 

    /** 
    * now we can actually use media_handle_sideload 
    * we pass it the file array of the file to handle 
    * and the post id of the post to attach it to 
    * $post_id can be set to '0' to not attach it to any particular post 
    */ 
    $post_id = '0'; 

    $id = media_handle_sideload($file_array, $post_id); 

    /** 
    * We don't want to pass something to $id 
    * if there were upload errors. 
    * So this checks for errors 
    */ 
    if (is_wp_error($id)) { 
     @unlink($file_array['tmp_name']); 
     return $id; 
    } 

    /** 
    * No we can get the url of the sideloaded file 
    * $value now contains the file url in WordPress 
    * $id is the attachment id 
    */ 
    $value = wp_get_attachment_url($id); 

// Now you can do something with $value (or $id) 

    return $id; 

} 
+0

如果您確實使用url請求圖片的路徑是正確的,您是否有'.htaccess'文件?如果是,請將其內容發佈在問題中。 –

+1

使用media_sideload_image wordpress函數 – onlinewebsite

+0

@onlinewebsite這和我用過的media_handle_sideload函數有什麼區別? – Kyon147

回答

0

所以這是一個非常愚蠢的答案,但...

我改變了默認本地URL,因爲我想讓它顯示來自現場的圖像。將其設置回本地上傳文件夾位置開始正確顯示圖像。

發表了答案,因爲別人提出了同樣的問題。檢查您的設置>媒體>文件的完整URL路徑。

謝謝大家!

0

搜索並從谷歌下載插件..

這個插件「從URL導入」取代了內置的媒體 上傳標籤,讓你從遠程URL 下載圖像成WordPress媒體庫。 遠程圖像的縮略圖在本地創建,上傳後您可以添加自定義媒體屬性(如標題,標題)和 將圖片插入到您的博客文章中。 如何找到插件: 當編輯帖子時,點擊「添加媒體」 然後選擇「保存&從URL導入」:如果您的窗口是 大,這是在左邊的邊欄。如果您的窗口很小,您需要選擇預先選擇「插入 媒體」的下拉菜單。 功能列表: 直接將遠程圖像包含到博客文章中,無需 必須將其保存到您的計算機本地 成爲更好的互聯網公民:避免與 輕鬆連接圖像!下載 與媒體庫全面整合後 重命名圖像 - 包括創建 縮略圖..

像&份額

相關問題