2011-12-22 69 views
3

我試圖以編程方式添加一堆帖子,然後添加其伴隨圖像。我有一個包含所有圖像文件名的數組,並且我已經能夠將它們添加到數據庫中,但我似乎無法獲得創建的正確縮略圖。試圖以編程方式生成WordPress縮略圖

帖子來自位於wp-content/uploads/dirname /中的csv。它們的文件名中包含的數字與CSV中的ID相對應,這就是我如何知道需要將哪些圖像添加到後綴ID中。

我已經得到了wp_insert_attachment()部分來處理它們在他們自己的小目錄中的圖像,但我無法得到縮略圖來生成。我安裝了再生縮略圖插件,並能夠生成它們,但似乎無法通過編程方式實現。

我認爲這可能是因爲wp_generate_attachment需要將照片放在/ uploads/2011/12 /(例如)中,所以我開始沿着移動圖像然後嘗試添加它們的路徑。無論如何,這是有道理的,因爲我想要製作副本而不是將5或6種不同的介質尺寸添加到我的wp-content/uploads/dirname/dir中。

無論如何,它不起作用。通過PHP副本移動圖像不起作用,縮略圖不會生成。

foreach ($frazerfiles[$stock_num] as $stock_img){ 
    require_once(ABSPATH . 'wp-admin/includes/image.php'); 
    echo "... ...Trying to add attachment metadata..."; 

    // copy the file to the upload dir 
    $uploads = wp_upload_dir(); 
    $file_to_move = ABSPATH."wp-content/uploads/".$stock_img; 
    if (copy($file_to_move, $uploads['path']."/")) { 
    echo "Moved $file_to_move to ".$uploads['path']."/"; 
    $my_moved_file = $uploads['path']."/".$stock_img; 

    // I think this is failing because the images aren't in the upload dir. 
    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 
    if ($attach_id = wp_insert_attachment($attachment, $my_moved_file, $newpostid)) { 
     if ($attach_data = wp_generate_attachment_metadata($attach_id, $my_moved_file)) { 
     echo "...success for $my_moved_file: ID:$attach_id<br />\n"; 
     } else { 
     echo "...FAILED for $my_moved_file ID:$attach_id<br />\n"; 
     print_r($attach_data); 
     } 
    } else { // inserting attachment failed 
     echo "Insert attachment failed for $my_moved_file to $newpostid<br />\n"; 
    } 
    } else { 
    echo "Failed moving $file_to_move to ".$uploads['path']."/"; 
    } 


}// images foreach 

回答

3

後與WordPress核心文件的交叉檢查,這似乎是缺掉的唯一事情就是wp_update_attachment_metadatawp_generate_attachment_metadata之後。

wp_update_attachment_metadata($attach_id, $attach_data); 

嘗試增加

echo "...success for $my_moved_file: ID:$attach_id<br />\n"; 

所以如果塊看起來像

if ($attach_data = wp_generate_attachment_metadata($attach_id, $my_moved_file)) { 
    echo "...success for $my_moved_file: ID:$attach_id<br />\n"; 
    wp_update_attachment_metadata($attach_id, $attach_data); 
    } else { 
    echo "...FAILED for $my_moved_file ID:$attach_id<br />\n"; 
    print_r($attach_data); 
    } 

建議:(不涉及這個問題)

移動線

require_once(ABSPATH . 'wp-admin/includes/image.php'); 

以上(for above)循環。

更新1: 添加建議來解決複製問題。 您錯過了「檢查文件類型」行。 (如果目標文件($my_moved_file)已經存在PHP複製功能會失敗)

改變此密碼

// copy the file to the upload dir 
$uploads = wp_upload_dir(); 
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img; 
if (copy($file_to_move, $uploads['path']."/")) { 
echo "Moved $file_to_move to ".$uploads['path']."/"; 
$my_moved_file = $uploads['path']."/".$stock_img; 

// I think this is failing because the images aren't in the upload dir. 
$attachment = array(
    'post_mime_type' => $wp_filetype['type'], 
    'post_title' => preg_replace('/\.[^.]+$/', '', basename($stock_img)), 
    'post_content' => '', 
    'post_status' => 'inherit' 
); 

TO

// copy the file to the upload dir 
$uploads = wp_upload_dir(); 
$file_to_move = ABSPATH."wp-content/uploads/".$stock_img; 
$my_moved_file = $uploads['path']."/".$stock_img; 
if (copy($file_to_move, $my_moved_file)) { 
    echo "Moved $file_to_move to ".$my_moved_file; 

    // Check the file type 
    $wp_filetype = wp_check_filetype(basename($my_moved_file), null); 

    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($my_moved_file)), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 
+0

嗯,你說得對。感謝您的建議。但是,複製部分不起作用。那裏有任何想法? – 2011-12-23 14:30:44

+1

更新後的答案,請在更新1後檢查: – tamilsweet 2011-12-23 15:01:12

+0

我最終將複製內容移出到它自己的函數中,現在它可以工作。這個問題最終導致PHP的copy()不像shell中的cp,因爲第二個參數需要是目標目錄**中的文件名**,而不僅僅是目錄名。 so path/to/file/filename.ext不只是路徑/到/文件 – 2011-12-23 16:52:19