我一直在尝试各种方法。基本上,我一直在尝试这样做,以便我的一个插件在插件激活期间只填充一次分类法的术语。术语填充是通过wp\\u insert\\u terms函数在函数中完成的。直接在register\\u activation\\u钩子内调用函数似乎不起作用,使用register\\u activation\\u钩子钩住init钩子也不起作用。有人有什么想法吗?
这是我的代码版本
//version 1
class vsetup {
function __construct() {
register_activation_hook(__FILE__,array($this,\'activate\'));
$this->create_taxonomies();
}
function activate() {
wp_insert_term(\'Action\',\'genre\');
wp_insert_term(\'Adventure\',\'genre\');
}
function create_taxonomies() {
$genre_args = array(
\'hierarchical\' => true,
\'labels\' => array(
\'name\'=> _x(\'Genres\', \'taxonomy general name\' ),
\'singular_name\' => _x(\'Genre\', \'taxonomy singular name\'),
\'search_items\' => __(\'Search Genres\'),
\'popular_items\' => __(\'Popular Genres\'),
\'all_items\' => __(\'All Genres\'),
\'edit_item\' => __(\'Edit Genre\'),
\'edit_item\' => __(\'Edit Genre\'),
\'update_item\' => __(\'Update Genre\'),
\'add_new_item\' => __(\'Add New Genre\'),
\'new_item_name\' => __(\'New Genre Name\'),
\'separate_items_with_commas\' => __(\'Seperate Genres with Commas\'),
\'add_or_remove_items\' => __(\'Add or Remove Genres\'),
\'choose_from_most_used\' => __(\'Choose from Most Used Genres\')
),
\'query_var\' => true,
\'rewrite\' => array(\'slug\' =>\'genre\')
);
register_taxonomy(\'genre\', \'post\',$genre_args);
}
}
当这不起作用时,我试着这样做:
//version 2
class vsetup {
function __construct() {
register_activation_hook(__FILE__,array($this,\'activate\'));
$this->create_taxonomies();
}
function activate() {
add_action(\'init\', array($this,\'populate_taxonomies\'));
}
function create_taxonomies() {
$genre_args = array(
\'hierarchical\' => true,
\'labels\' => array(
\'name\'=> _x(\'Genres\', \'taxonomy general name\' ),
\'singular_name\' => _x(\'Genre\', \'taxonomy singular name\'),
\'search_items\' => __(\'Search Genres\'),
\'popular_items\' => __(\'Popular Genres\'),
\'all_items\' => __(\'All Genres\'),
\'edit_item\' => __(\'Edit Genre\'),
\'edit_item\' => __(\'Edit Genre\'),
\'update_item\' => __(\'Update Genre\'),
\'add_new_item\' => __(\'Add New Genre\'),
\'new_item_name\' => __(\'New Genre Name\'),
\'separate_items_with_commas\' => __(\'Seperate Genres with Commas\'),
\'add_or_remove_items\' => __(\'Add or Remove Genres\'),
\'choose_from_most_used\' => __(\'Choose from Most Used Genres\')
),
\'query_var\' => true,
\'rewrite\' => array(\'slug\' =>\'genre\')
);
register_taxonomy(\'genre\', \'post\',$genre_args);
}
function populate_taxonomies() {
wp_insert_term(\'Action\',\'genre\');
wp_insert_term(\'Adventure\',\'genre\');
}
}
到目前为止,这两个想法对我都不起作用。
最合适的回答,由SO网友:Hameedullah Khan 整理而成
这是您的代码的固定版本。
class vsetup {
function __construct() {
register_activation_hook(__FILE__,array($this,\'activate\'));
add_action( \'init\', array( $this, \'create_taxonomies\' ) );
}
function activate() {
$this->create_taxonomies();
wp_insert_term(\'Action\',\'genre\');
wp_insert_term(\'Adventure\',\'genre\');
}
function create_taxonomies() {
$genre_args = array(
\'hierarchical\' => true,
\'labels\' => array(
\'name\'=> _x(\'Genres\', \'taxonomy general name\' ),
\'singular_name\' => _x(\'Genre\', \'taxonomy singular name\'),
\'search_items\' => __(\'Search Genres\'),
\'popular_items\' => __(\'Popular Genres\'),
\'all_items\' => __(\'All Genres\'),
\'edit_item\' => __(\'Edit Genre\'),
\'edit_item\' => __(\'Edit Genre\'),
\'update_item\' => __(\'Update Genre\'),
\'add_new_item\' => __(\'Add New Genre\'),
\'new_item_name\' => __(\'New Genre Name\'),
\'separate_items_with_commas\' => __(\'Seperate Genres with Commas\'),
\'add_or_remove_items\' => __(\'Add or Remove Genres\'),
\'choose_from_most_used\' => __(\'Choose from Most Used Genres\')
),
\'query_var\' => true,
\'rewrite\' => array(\'slug\' =>\'genre\')
);
register_taxonomy(\'genre\', \'post\',$genre_args);
}
}
不要忘记创建vsetup类的对象/实例,即
new vsetup()
哼!忘记删除调试语句。