2014-09-03 60 views
0

我有這樣的代碼是工作的罰款插入內部在WordPress庫上傳照片並創建成功後集特色圖片WordPress的

,但沒有奏效設置上傳的照片爲特色圖像在帖子

注意:即時通訊使用自定義帖子類型。

我有嘗試噸的解決方案的網絡計算器上的任何內容制定

$dir = plugin_dir_path(__FILE__); 
    $file_path = $dir."/uploads/"; 
    $text = $_POST['text']; 
    $user = $_POST['usr']; 
    $file_path = $file_path . basename($_FILES['uploaded_file']['name']); 
    move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path); 


$file = $file_path; 
$filename = basename($file); 

$upload_file = wp_upload_bits($filename, null, file_get_contents($file)); 
if (!$upload_file['error']) { 
    $wp_filetype = wp_check_filetype($filename, null); 
    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_parent' => $parent_post_id, 
     'post_title' => preg_replace('/\.[^.]+$/', '', $filename), 
     'post_content' => '', 
     'post_status' => 'inherit' 
    ); 
    $attachment_id = wp_insert_attachment($attachment, $upload_file['file'], $parent_post_id); 
    if (!is_wp_error($attachment_id)) { 
     require_once(ABSPATH . "wp-admin" . '/includes/image.php'); 
     $attachment_data = wp_generate_attachment_metadata($attachment_id, $upload_file['file']); 
     wp_update_attachment_metadata($attachment_id, $attachment_data); 
    } 
} 

unlink($file_path); 

$user = get_userdatabylogin($_POST['usr']); 
$user_ID = $user->ID; // prints the id of the user 

global $user_ID; 

$new_post = array(
'post_title' => 'My New Post', 
'post_content' => $_POST['text'], 
'post_status' => 'publish', 
'post_date' => date('Y-m-d H:i:s'), 
'post_author' => $user_ID, 
'post_type' => 'taken_photos', 
'post_category' => array(0) 
); 
$pid = wp_insert_post($new_post); 

    update_post_meta($pid, '_thumbnail_id', $attachment_id); 

    $attachment_data = array(
    'ID' => $attachment_id, 
    'post_excerpt' => 'TITLE' 
); 

回答

0
$file_path = $dir."/uploads/".date("Y")."/".date("m"); 

能否請你做出這樣that.Hope文件路徑,它將爲你工​​作。你行之後

+0

的'$ file_path'無關,用它做的也是這個文件的路徑是暫時的,如果你好好看看的代碼你會看到,即時消息插入wordpress媒體庫後刪除這個文件 – 2014-09-03 17:49:51

0

wp_update_attachment_metadata($attachment_id, $attachment_data); 

添加額外的行

add_post_meta($parent_post_id, '_thumbnail_id', $attachment_id); 
相關問題