参考: http://codex.wordpress.org/Function_Reference/wp_insert_post
如果您看到上面的链接,则明确表示您可以在wp_insert_post中分类ID:
'post_category' => [ array(<category id>, ...) ] // Default empty.
但是我想添加变量
$cat=Samsung; //keep on changing
'post_category' => $cat;
使用 get_cat_ID (http://codex.wordpress.org/Function_Reference/get_cat_ID)
如果只是一个类别:
$cat=get_cat_ID( 'Samsung' );
'post_category' => array($cat)
否则,您将需要执行 foreach 并创建数组。
使用变量是可以的。根据您引用的行,类别应该是类别 ID 数组。因此,在您的示例中,您应该尝试:
'post_category' => array($cat)