如何设置帖子的类别名称:这是我的代码,这不起作用,为什么?
$post_title = $vname;
$categ=\'category name\';
$post_content = \'[newpage link="\'.$videos.\'"]\';
$new_post = array(
\'ID\' => \'\',
\'post_author\' => $user->ID,
\'post_category\' => $categ,
\'post_content\' => $post_content,
\'post_title\' => $post_title,
\'post_status\' => \'publish\',
);
$post_id = wp_insert_post($new_post);
SO网友:Venkatesh Munna
检查此。。这可能会帮助你“玩具”=>“孩子”=>“鼻涕虫”=>“猫名”,
因此,如果您可以在此处添加类别名称,它将为您的所有帖子指定特定类别。这是你所需要的吗。
检查此项以供参考:https://codex.wordpress.org/Function_Reference/wp_set_post_categories
http://wordpress-code-snippets.blogspot.in/2014/05/how-to-assing-category-to-post.html
Copy and Paste the Below code in you theme functions.php.
function auto_add_category ($post_id = 0) {
if (!$post_id) return;
$tag_categories = array ( // add multiple items as shown \'Tag Name\' => \'Categroy Name\'
\'toys\' => \'Kids\',
\'apple\' => \'Fruits\',
);
$post_tags = get_the_tags($post_id);
foreach ($post_tags as $tag) {
if ($tag_categories[$tag->name] ) {
$cat_id = get_cat_ID($tag_categories[$tag->name]);
if ($cat_id) {
$result = wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = \'category\', $append = true );
}
}
}
}
add_action(\'publish_post\',\'auto_add_category\');
?>