您必须首先使用wp\\U insert\\U term根据需要插入术语。
wp_set_object_terms( $post_id, \'Lense\', \'product_cat\', false);
此函数检查product\\u cat中是否存在“Lense”术语。如果不存在,则创建并分配。检查术语是否已存在
$id = term_exists( \'canon\',\'product_cat\',$parent_id ) //$parent_id is parent term id, in your case id of Lense
if($id == NULL)
{
$id = wp_insert_term(
\'Canon\', // the term
\'product_cat\', // the taxonomy
array(
\'description\'=> \'Test description\',
\'slug\' => \'canon\',
\'parent\'=> $parent_term[\'term_id\'] // get numeric term id of parent - Lense
)
);
}
wp_set_object_terms( $post_id,$id, \'product_cat\',true);
这将起作用
//分类名称中不允许有空格