本段程式是自己改寫於新增portfolio文章與新增post文章有點不同
function auto_post(){
$postData = array(
'post_title' => 'My portfolio post',
'post_content' => '',
'post_type' => 'dt_portfolio', // Default 'post',預設值是post
'post_status' => 'publish',
'post_author' => 1
);
// 新增文章並取得post_id
$post_id = wp_insert_post( $postData );
$getImageFile = ABSPATH . 'wp-content/uploads/2015/11/4A7C3296.jpg';// 圖片來源
$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 );
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 );
// 設定portfolio分類(註)
wp_set_post_terms($post_id, 22, 'dt_portfolio_category');
}
註:假如是文章的話不需在此設定分類,$postData那邊加入以下程式'post_category' => array( $Category ),即可,因為筆者要新增的是portfolio分類所以無法使用該方法,故用wp_set_post_terms()來修改該筆post分類。
訂閱:
張貼留言 (Atom)
[Laravel]環境架設,使用docker + laradock
1.選擇使用docker + laradock在windows10的環境使用 先至 docker官方網站 下載 docker for windows 2.依照執行程式下載安裝 這邊我的電腦有遇到一些問題順便記錄下來, 在下載啟動docker時發生錯誤 Hardw...

-
在C:\xampp\mysql\bin目錄中找到my.ini檔 用文字編輯器打開後 找到[client] 增加 default-character-set=utf8 找到[mysqld] 增加 character-set-server=utf8 collation...
-
/// <summary>以亂數生成新密碼</summary> /// <returns>回傳認證碼</returns> public string CreateNewPasswo...
-
1.選擇使用docker + laradock在windows10的環境使用 先至 docker官方網站 下載 docker for windows 2.依照執行程式下載安裝 這邊我的電腦有遇到一些問題順便記錄下來, 在下載啟動docker時發生錯誤 Hardw...
沒有留言:
張貼留言