我正在建立一个wordpress主题,人们可以使用wp_insert_post提交帖子。下面的代码添加了文章标题,但没有添加用户指定的类别。相反,它将其置于未分类状态。如何在提交时添加用户提交的类别?
<?php
if(isset($_POST[\'new_post\']) == \'1\') {
$post_title = $_POST[\'post_title\'];
$new_cat_ID = $_POST[\'category\'];
//Checking if category already there
$cat_ID = get_cat_ID( $_POST[\'newcat\'] );
//If not create new category
if($cat_ID == 0) {
$cat_name = array(\'cat_name\' => $_POST[\'newcat\']);
wp_insert_category($cat_name);
}
//Get ID of newly created category
$new_cat_ID = get_cat_ID($_POST[\'newcat\']);
// Create post object
$new_post = array(
\'ID\' => \'\',
\'post_title\' => $post_title,
\'post_status\' => \'publish\',
\'post_author\' => $user->ID,
\'tax_input\' => array( \'category\' => $new_cat_ID )
);
// Insert the post into the database
$post_id = wp_insert_post($new_post);
// This will redirect you to the newly created post
$post = get_post($post_id);
wp_redirect( home_url() ); exit;
}
?>
这是html表单。。。
<form style="" action="" method="post" id="foo">
<input type="hidden" name="new_post" value="1"/>
<input type="text" name="post_title" value="http://<?php echo $seal; ?>" id="input-title"/>
<input type="text" name="post_category" value="2" id="post_category" />
<input type="submit" value="Login">
</form>