问题描述
我一直在尝试各种各样的方法。基本上,我一直在试图让这个插件在插件的激活期间只填写一次分类法的一个插件。术语填充是通过 wp_insert_terms 函数在函数中完成的。在 register_activation_hook 中直接调用函数似乎不起作用,也不使用 register_activation_hook 挂起 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');
}
}
到目前为止,这两种想法对我来说都是有效的。
最佳解决方案
这是您的代码的固定版本。
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()
咦!忘了删除调试语句。
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。