亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 WooCommerce 中的當前產品類別獲取同級術語 ID 列表

從 WooCommerce 中的當前產品類別獲取同級術語 ID 列表

PHP
繁花如伊 2023-12-15 15:06:58
我想根據當前類別 ID 檢索術語 ID 列表。目前我正在使用以下代碼:$product_cat_items  = get_queried_object();$product_cat_id     = $product_cat_items->term_id;$product_cat_child  = get_term($product_cat_id, 'product_cat');$product_cat_parent = $product_cat_child->parent;$product_cat_related= get_terms('product_cat', array( 'parent' => $product_cat_parent, 'exclude' => $product_cat_id ));它正在工作,我得到了一系列術語。 但問題是,我只需要術語對象中的 ID 即可獲取如下列表:123,345,678有沒有辦法從$product_cat_related數組中提取這樣的列表?這是當前的輸出:array(2) {  [0]=>  object(WP_Term)#26238 (10) {    ["term_id"]=>    int(177)    ["name"]=>    string(27) "Name"    ["slug"]=>    string(21) "name"    ["term_group"]=>    int(0)    ["term_taxonomy_id"]=>    int(177)    ["taxonomy"]=>    string(11) "product_cat"    ["description"]=>    string(0) ""    ["parent"]=>    int(140)    ["count"]=>    int(8)    ["filter"]=>    string(3) "raw"  }  [1]=> ....}
查看完整描述

2 回答

?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

自 WordPress 4.5.0 版起,分類法應通過“taxonomy”傳遞$args?數組中的參數。
另外,當查詢的對象是分類術語時,get_queried_object()?已經給出了WP_Term?對象。
您還可以使用?'fields' => 'ids'?作為?get_terms()?中的參數,以僅獲取?term Ids 數組?而不是?WP_term?對象數組?。
最后,您將使用 PHP?implode()?獲取一串逗號分隔的術語 ID。

所以你的代碼將是:

$current_term = get_queried_object(); // Already a WP_Term Object


if ( $current_term->parent > 0 ) {

? ? $siblings_ids = get_terms( array(

? ? ? ? 'taxonomy'? => 'product_cat',

? ? ? ? 'parent'? ? => $current_term->parent,

? ? ? ? 'exclude'? ?=> $current_term->term_id,

? ? ? ? 'fields'? ? => 'ids',

? ? ) );


? ? // Get a string of coma separated terms Ids

? ? $siblings_list_ids = implode(',', $siblings_ids);


? ? // Testing output

? ? echo $siblings_list_ids;

}

經過測試并有效。


查看完整回答
反對 回復 2023-12-15
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

對我來說這段代碼有效:

$idCats?=?array_column($product_cat_related,?'term_id');


查看完整回答
反對 回復 2023-12-15
  • 2 回答
  • 0 關注
  • 180 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號