在帖子中插入表格中的类别

时间:2017-02-11 作者:Rosanna La Malfa

当类别来自表单时,如何在wp\\u insert\\u post中使用?

$my_post = array(); 
$my_post[\'post_author\'] = $userid; 
$my_post[\'post_title\'] = $name; 
$my_post[\'post_name\'] = str_replace(\' \', \'-\', $name); 
$my_post[\'post_category\'] = $selected = $_POST[\'postcats\']; 
$newpost_id= wp_insert_post( $my_post );
是否正确?怎么了?非常感谢

1 个回复
SO网友:David Lee

您需要先创建类别,然后将其添加到帖子中:

$my_post = array(); 
$my_post[\'post_author\'] = $userid; 
$my_post[\'post_title\'] = $name; 
$my_post[\'post_name\'] = str_replace(\' \', \'-\', $name);
$cat_id = wp_create_category($_POST[\'postcats\'], 0); // we create the category and we get the ID, the 0 is for no parent category
$my_post[\'post_category\'] = array($cat_id);//this value is an array not a single value 
$newpost_id= wp_insert_post( $my_post );
您也可以创建帖子,稍后使用添加类别wp_set_post_categories

相关推荐