我正在使用wp_insert_post()函数创建具有自定义数据的帖子, 但是我无法在帖子中添加类别和特色图片, 我的代码如下:
$new_post = array(
'post_title' => $leadTitle, 'post_content' => $leadContent, 'post_status' => $postStatus, 'post_date' => $timeStamp, 'post_author' => $userID, 'post_type' => $postType, 'post_category' => array('language'), 'tags_input' => array('Language', "Temp")
);
$post_id = wp_insert_post($new_post);
有什么问题吗?
#1
如果你的类别具有层次结构(对于默认帖子类型, 则为wp_insert_post()的post_category参数应作为类别ID的数组提供):
'post_category' => array(90, 100)
另外, 请确保ID在提供为int的数组中。
你可以在文档中找到更详细的信息。
如果你将图片添加到网站的媒体库中, 则可以使用其ID(例如)将其用作帖子/页面的附件:
'_thumbnail_id' => 110
如果没有它, 那么首先需要使用wp_insert_attachment()函数将图像添加为附件类型。
因此, 你的函数将如下所示:
$new_post = array(
'post_title' => $leadTitle, 'post_content' => $leadContent, 'post_status' => $postStatus, 'post_date' => $timeStamp, 'post_author' => $userID, 'post_type' => $postType, 'post_category' => array(99), 'tags_input' => array('Language', "Temp"), '_thumbnail_id' => 110
);
$post_id = wp_insert_post($new_post);
#2
Please check below code:
$my_post = array();
$my_post['post_title'] = 'My post';
$my_post['post_content'] = 'This is my post.';
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
$my_post['post_category'] = array(0);
// Insert the post into the database
wp_insert_post( $my_post );"
Or
$new_post = array(
'post_title' => $leadTitle, 'post_content' => $leadContent, 'post_status' => $postStatus, 'post_date' => $timeStamp, 'post_author' => $userID, 'post_type' => $postType, 'post_category' => array('language'), 'tags_input' => array('Language', "Temp")
);
$post_id = wp_insert_post($new_post);
评论前必须登录!
注册