如何在WooCommerce中设置子类别?

时间:2019-01-29 作者:Yves Gonzaga

您好,我想问一下,我们如何通过编程在wordpress中设置帖子的子类别?

wp_set_object_terms( $post_id, \'Lense\', \'product_cat\', false);
wp_set_object_terms( $post_id, \'Canon\', \'product_cat\', true);
但它所做的并不是将父类别和子类别都设置为父类别

1 个回复
最合适的回答,由SO网友:Aparna_29 整理而成

您必须首先使用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); 
这将起作用
//分类名称中不允许有空格

相关推荐