跟踪return value of wp_insert_term
并用它来构建你的结构。成功的返回值将是具有term_id
可以传递到的密钥wp_insert_term
\'s$args
数组作为parent
.
$parent = wp_insert_term(\'Science\', \'category\'); // I\'ll leave out `$args` here
if (is_wp_error($parent)) {
// insert didn\'t work! return from your function or error or whatever
return;
}
// grade 11 category has "Sience" as its parent
$grade11 = wp_insert_term(\'Grade 11\', \'category\', [\'parent\' => $parent[\'term_id\']]);
if (is_wp_error($grade11)) {
// same as above, something went wrong
return;
}
// Management has the parent "Grade 11"
wp_insert_term(\'Management\', \'category\', [\'parent\' => $grade11[\'term_id\']]);
只要插入一次,就可以钩住
after_switch_theme
做你的事。您可能希望首先查找您的术语,以确保它没有被插入,或者在成功时使用插入的术语ID设置一个选项。
add_action(\'after_switch_theme\', \'wpse206511_switch\');
function wpse206511_switch()
{
if (get_option(\'wpse206511_term_id\')) {
// already done, bail
return;
}
// category insert code from above
// set the term ID in an option that you can check later
// if/when the theme gets activated/deactivated again
add_option(\'wpse206511_term_id\', $parent[\'term_id\']);
}