2017-06-21 74 views
0

我試圖根據woocommerce中給定的父產品類別顯示所有子產品類別的數據。我可以通過WordPress功能獲得一系列產品類別ID,get_term_childrenWordPress的get_category不工作forloop

$category_children = get_term_children(25, 'product_cat'); 

foreach ($category_children as $category_id) { 
    echo '<br> cat ID' . $category_id; 

    echo '<pre>'; 
    print_r(get_category($category_id)); 
    echo '</pre>'; 
} 

問題是當我嘗試在循環中顯示該數據。在這種情況下,我有兩個子產品類別。我的循環只會返回除最後一個ID以外的所有數據。這是我得到的。奇怪的是,我可以看到循環抓取所有來自$category_children的ID。

cat ID 26 
WP_Term Object 
(
    [term_id] => 26 
    [name] => Shirts 
    [slug] => shirts 
    [term_group] => 0 
    [term_taxonomy_id] => 26 
    [taxonomy] => product_cat 
    [description] => 
    [parent] => 25 
    [count] => 2 
    [filter] => raw 
    [meta_value] => 0 
    [cat_ID] => 26 
    [category_count] => 2 
    [category_description] => 
    [cat_name] => Shirts 
    [category_nicename] => shirts 
    [category_parent] => 25 
) 
cat ID 27 

哪裏是爲27日凌晨WP_Term對象我沒有使用正確get_category或者,我需要取消它還是什麼?

回答

1

如果你看看get_category()documentation,你可以看到它使用函數get_term()和分類「category」。

而不是使用get_category獲取你的足月兒,用get_term()直接想:

$category_children = get_term_children(25, 'product_cat'); 
    foreach ($category_children as $category_id) { 
     echo '<br> cat ID' . $category_id; 

     echo '<pre>'; 
     print_r(get_term($category_id, 'product_cat')); 
     echo '</pre>'; 
    } 
} 

而且get_term($category_id)不分類 'product_cat' 作品。