[英]How to add a product category in woocommerce wordpress
我真的希望有人能盡快幫助我解決這個問題。
好的,所以我正在為用戶構建一個用於發布新產品的 Costum 腳本,我讓一切正常工作並成功插入(事件照片)但似乎無法在任何地方找到應該使用什么代碼來更新帖子類別,因為它不是一個普通類別,因為它具有“product_cat”(woocommerce 產品類別)的分類法。
有任何想法嗎?
非以下工作:($term_id 是與某個產品的“product_cat”相關的 term_id)
wp_set_post_terms($post_id, array($term_id), "product_cat"); wp_set_post_terms($post_id, (int)$term_id, "product_cat"); update_post_meta($post_id,'product_cat',$term_id);
我試過其他人,但他們似乎根本沒有做任何事情,有些功能甚至創建了一個帶有 id 的新類別......
您可以使用:
wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false )
參數:
$post_id (int) (可選)帖子 ID。 不默認為全局 $post 的 ID。
$tags (string|array) (可選)要為帖子設置的術語數組,或用逗號分隔的術語字符串。 分層分類法必須始終傳遞 ID 而不是名稱,以便具有相同名稱但不同父母的孩子不會混淆。 默認值: ''
$taxonomy(字符串)(可選)分類名稱。
默認值:'post_tag'
$append (bool) (可選)如果為 true,則不要刪除現有術語,只需添加即可。 如果為 false,則用新條款替換條款。
默認值:false
在此處閱讀更多相關信息: https : //developer.wordpress.org/reference/functions/wp_set_post_terms/
嘗試使用wp_insert_term
:
wp_insert_term(
'New Category', // the term
'product_cat', // the taxonomy
array(
'description'=> 'Category description',
'slug' => 'new-category'
)
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.