2011-08-05 103 views
2

我的問題是關於使用wp_insert_post插入類型爲「照片」的帖子 現在不需要上傳圖片只是我想插入帖子信息到數據庫中 我用這個代碼和它正在如何插入照片使用wp_insert_post

$my_post = array(

    'post_title' => 'My post', 
    'post_content' => 'This is my post.', 
    'post_status' => 'publish', 
    'post_author' => 1, 
     'post_type' => 'photo', 
    'post_category' => array(3) 
); 

問題是我想補充以下信息 1-照片類型 2 - 我想設置的崗位作爲特色圖片

回答

0

您首先需要上傳的文件,那麼你可以附加圖像作爲attac感謝您的信息。這是我用來自動化博客的代碼:

$wp_filetype = wp_check_filetype(basename($filename), null); 
$attachment = array(
'post_mime_type' => $wp_filetype['type'], 
'post_title' => $postTitle, 
'post_content' => '', 
'post_status' => 'inherit' 
); 
$attach_id = wp_insert_attachment($attachment, $filename, $postId); 
// you must first include the image.php file 
// for the function wp_generate_attachment_metadata() to work 
require_once(ABSPATH . "wp-admin" . '/includes/image.php'); 
$attach_data = wp_generate_attachment_metadata($attach_id, $filename); 
wp_update_attachment_metadata($attach_id, $attach_data);