通过自定义插件创建分类法时如何创建分类法术语?
How to create taxonomy term when creating the taxonomy through a custom plugin?
我正在尝试在创建分类法时实用地添加分类法术语。我尝试添加
wp_insert_term(
'A00', // the term
'we_colors'
);
到 tax_color_palletes
但这不是添加术语 A00
。我正在研究这个片段;我该如何解决这个问题?
function tax_color_palletes() {
$labels = array(
'name' => 'Models',
'singular_name' => 'Model',
'menu_name' => 'Models',
'all_items' => 'All Models',
'parent_item' => 'Parent Model',
'parent_item_colon' => 'Parent Model',
'new_item_name' => 'Model Name',
'add_new_item' => 'Add New Model',
'edit_item' => 'Edit Model',
'update_item' => 'Update Model Type',
'separate_items_with_commas' => 'Separate Model with commas',
'search_items' => 'Search Models',
'add_or_remove_items' => 'Add or remove Model Type',
'choose_from_most_used' => 'Choose from the most used Model',
'not_found' => 'Model Not Found',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'women_models', array( 'we_colors' ), $args );
wp_insert_term(
'A00', // the term
'we_colors'
);
}
add_action( 'init', 'tax_color_palletes', 0 );
您是否尝试将 term
添加到您的 taxonomy
或 post_type
?
在您的示例中,您将 taxonomy
'women_models' 注册到 post_type
'we_colors'。
但是随后您调用 wp_insert_term
(这需要一个 taxonomy
)并传递给它一个 post_type
。这应该会给你一个错误。
如果您只想将 term
添加到 taxonomy
,您需要将 taxonomy
传递给 wp_insert_term
。
wp_insert_term('A00', 'women_models');
如果您实际上是在尝试向 post_type 添加术语,则应使用 wp_set_object_terms
,后者又可以调用 wp_insert_term
本身来创建新术语。但是,您需要先获得 $object_id
。
wp_set_object_terms( $object_id, ['term_1', 'term_2'], 'taxonomy_name');
https://developer.wordpress.org/reference/functions/register_taxonomy/
https://developer.wordpress.org/reference/functions/wp_insert_term/
https://developer.wordpress.org/reference/functions/wp_set_object_terms/
我正在尝试在创建分类法时实用地添加分类法术语。我尝试添加
wp_insert_term(
'A00', // the term
'we_colors'
);
到 tax_color_palletes
但这不是添加术语 A00
。我正在研究这个片段;我该如何解决这个问题?
function tax_color_palletes() {
$labels = array(
'name' => 'Models',
'singular_name' => 'Model',
'menu_name' => 'Models',
'all_items' => 'All Models',
'parent_item' => 'Parent Model',
'parent_item_colon' => 'Parent Model',
'new_item_name' => 'Model Name',
'add_new_item' => 'Add New Model',
'edit_item' => 'Edit Model',
'update_item' => 'Update Model Type',
'separate_items_with_commas' => 'Separate Model with commas',
'search_items' => 'Search Models',
'add_or_remove_items' => 'Add or remove Model Type',
'choose_from_most_used' => 'Choose from the most used Model',
'not_found' => 'Model Not Found',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'women_models', array( 'we_colors' ), $args );
wp_insert_term(
'A00', // the term
'we_colors'
);
}
add_action( 'init', 'tax_color_palletes', 0 );
您是否尝试将 term
添加到您的 taxonomy
或 post_type
?
在您的示例中,您将 taxonomy
'women_models' 注册到 post_type
'we_colors'。
但是随后您调用 wp_insert_term
(这需要一个 taxonomy
)并传递给它一个 post_type
。这应该会给你一个错误。
如果您只想将 term
添加到 taxonomy
,您需要将 taxonomy
传递给 wp_insert_term
。
wp_insert_term('A00', 'women_models');
如果您实际上是在尝试向 post_type 添加术语,则应使用 wp_set_object_terms
,后者又可以调用 wp_insert_term
本身来创建新术语。但是,您需要先获得 $object_id
。
wp_set_object_terms( $object_id, ['term_1', 'term_2'], 'taxonomy_name');
https://developer.wordpress.org/reference/functions/register_taxonomy/ https://developer.wordpress.org/reference/functions/wp_insert_term/ https://developer.wordpress.org/reference/functions/wp_set_object_terms/