我会提供一个答案,这样将来有同样问题的人可能会得到帮助。。。
The problem wasn\'t in the actual function. It was about when the taxonomy (categorycourses) was registered...
注册自定义岗位类型课程和课程类别的功能:
function courses_with_cats() {
//Register custom post type for courses
$course_labels = array(
\'name\' => \'Kurser\',
\'singular_name\' => \'Kurs\',
\'add_new\' => \'Lägg till\',
\'add_new_item\' => \'Lägg till ny kurs\',
\'edit_item\' => \'Redigera kurs\',
\'new_item\' => \'Ny kurs\',
\'all_items\' => \'Alla kurser\',
\'view_item\' => \'Visa kurs\',
\'search_items\' => \'Sök kurser\',
\'not_found\' => \'Inga kurser funna\',
\'not_found_in_trash\' => \'Inga kurser funna i sopkorgen\',
\'parent_item_colon\' => \'\',
\'menu_name\' => \'Kurser\'
);
$course_args = array(
\'labels\' => $course_labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'kurs\' ),
\'capability_type\' => \'post\',
\'has_archive\' => \'kurser\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\',\'editor\',\'author\',\'comments\'),
);
register_post_type( \'course\', $course_args );
//Register custom taxonomy for courses-categories
$course_cat_labels = array(
\'name\' => \'Kurskategori\',
\'singular_name\' => \'Kurskategori\',
\'search_items\' => \'Sök kategori\',
\'all_items\' => \'Alla kategori\',
\'parent_item\' => \'Föräldrakategori\',
\'parent_item_colon\' => \'Föräldrakategori:\',
\'edit_item\' => \'Redigera kategori\',
\'update_item\' => \'Uppdatera kategori\',
\'add_new_item\' => \'Lägg till ny kategori\',
\'new_item_name\' => \'Namn på ny kategori\',
\'menu_name\' => \'Kurskategori\'
);
$course_cat_args = array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'show_admin_column\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => \'kurser/kategori\')
);
register_taxonomy( \'categorycourses\', array(\'course\'), $course_cat_args );
}
BEFORE: (这不起作用)
add_action(\'init\', \'convertaddcoursesfromaccess\');
//Hooks for courses and courses-categories
add_action( \'init\', \'courses_with_cats\' );
AFTER: (这确实有效!)
//Hooks for courses and courses-categories
add_action( \'init\', \'courses_with_cats\' );
add_action(\'init\', \'convertaddcoursesfromaccess\');
(注意!!!在第一种情况下,即使帖子类型没有注册,也可以插入自定义帖子类型(课程)。但无法将此帖子与自定义分类法关联
wp_set_object_terms()
)
感谢Rahil Wazir让我意识到WP的功能wp_set_object_terms()
和wp_set_post_terms()
两者都适用于自定义帖子类型EVEN 如果Wordpress Codex在http://codex.wordpress.org/Function_Reference/wp_set_post_terms :
“此函数仅适用于本机帖子类型。对于自定义帖子类型的分类,请使用wp\\u set\\u object\\u terms()”
我已经确认事实并非如此,上面的两个工作函数都适用于我的自定义帖子类型(课程)。但我使用wp_set_object_terms()
因为我怀疑WP会wp_set_post_terms()
将来不推荐使用(不过这只是一个有保留的猜测)