是否可以向 woocommerce 帖子添加类别?
我正在创建我的产品如下:
// creates woocommerce product
$product = array(
'post_title' => $name,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' =>'product'
);
// Insert the post into the database
$product_ID = wp_insert_post($product);
我有一个名为“树”的类别,我必须将上述产品添加到其中。
我尝试过以下方法但没有成功。有什么特别的方法吗
添加类别?
wp_set_object_terms($productID, array('Tree'), 'product_cat');
经过一番尝试和错误后,我通过以下方式解决了它:
// Creates woocommerce product
$product = array(
'post_title' => $name,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' =>'product'
);
// Insert the post into the database
$product_ID = wp_insert_post($product);
// Gets term object from Tree in the database.
$term = get_term_by('name', 'Tree', 'product_cat');
wp_set_object_terms($product_ID, $term->term_id, 'product_cat');
更多信息参考:
-
如何以编程方式设置新的 Woocommerce 产品创建的类别? https://stackoverflow.com/questions/26690047/how-to-programatically-set-the-category-for-a--
- http://codex.wordpress.org/Function_Reference/get_term_by http://codex.wordpress.org/Function_Reference/get_term_by
- https://wordpress.stackexchange.com/questions/16394/how-to-get-a-taxonomy-term-name-by-the-slug https://wordpress.stackexchange.com/questions/16394/how-to-get-a-taxonomy-term-name-by-the-slug
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)