問題:下麵代碼能夠成功插入文章(post), 但是不能正常設置文章的Featured Image(特色圖像/縮略圖),代碼如下:
en: How to set featured image using wp_insert_post?
// Auto post ( Unique File Date ).
$postData = array(
'post_category' => array( $Category ),
'post_status' => $Post_Status,
'post_type' => $Post_Type
);
$post_id = wp_insert_post( $postData );
$getImageFile = 'http://localhost/Multisite/test2/wp-content/uploads/sites/4/Auto Post/twitter.png';
$attach_id = wp_insert_attachment( $postData, $getImageFile, $post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $getImageFile );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $post_id, $attach_id );
這是怎麽回事呢?
最佳解決方案:
需要使用不同的$postData作為附件。
代碼如下:
1. 先導入相關依賴:
require("/usr/share/nginx/html/xxxx". '/wp-load.php'); //注:/usr/share/nginx/html/xxxx是你的wordpress部署地址。
require_once( "/usr/share/nginx/html/xxxx" . '/wp-admin/includes/image.php' );
2. 然後按指定格式設置縮略圖(特色圖像):
$wp_filetype = wp_check_filetype( $getImageFile, null );
$attachment_data = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $getImageFile ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment_data, $getImageFile, $post_id );
隻有這樣正確設置附件插入函數wp_insert_attachment的參數,才能正確設置Featured Image。
3. 綁定縮略圖附件到文章。
//$attach_data = wp_generate_attachment_metadata( $attach_id, $getImageFile ); //注:不需要!
//wp_update_attachment_metadata( $attach_id, $attach_data ); //注:不需要!
//"隻需要下麵這行就可了!!!!!!"
set_post_thumbnail( $post_id, $attach_id );
值得一提的是,上麵的代碼整理之後,結合wordpress函數
wp_insert_post
和wordpress函數:
set_post_thumbnail
就可以完成自動批量給wordpress插入文章並設置縮略圖。
附1:wp_inset_post的參數說明
post = array(
'ID' => [ <post id> ] //需要更新的文章編號
'menu_order' => [ <order> ] //如果新文章是頁麵,設置顯示順序
'comment_status' => [ 'closed' | 'open' ] // 評論的狀態,'closed'關閉評論.
'ping_status' => [ 'closed' | 'open' ] // ping的狀態,'closed' 關閉 pingbacks和trackbacks
'pinged' => [ ? ] //該文章被ping到的地址
'post_author' => [ <user ID> ] //作者編號
'post_category' => [ array(<category id>, <...>) ] //文章歸類數組
'post_content' => [ <the text of the post> ] //文章內容,必填
'post_date' => [ Y-m-d H:i:s ] //文章編輯日期
'post_date_gmt' => [ Y-m-d H:i:s ] //文章編輯GMT日期
'post_excerpt' => [ <an excerpt> ] //摘要信息
'post_name' => [ <the name> ] // (slug) 文章別名
'post_parent' => [ <post ID> ] //新文章的父文章編號
'post_password' => [ ? ] //文章瀏覽密碼
'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' ] //新文章的狀態
'post_title' => [ <the title> ] //文章標題,必填
'post_type' => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] //文章類型:文章、頁麵、鏈接、菜單、其他定製類型
'tags_input' => [ '<tag>, <tag>, <...>' ] //標簽字符串
'to_ping' => [ ? ] //該文章需要ping到的地址
'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // 附加注釋數組 );
需要特別說明的是,參宿tags_input是直接填寫標簽中文(或英文)即可,數據wp_inset_post會通過wordpress數據看來做去重並轉化為關聯ID。
本文由《純淨的天空》整理自網路。
【參考文獻】
[1] http://stackoverflow.com/questions/28166651/set-featured-image-using-wp-insert-post
[2] http://www.cnblogs.com/xbdeng/p/5545180.html