我已經編寫了下面的函數來按類別id返回Worpress中所有類別的父級ID。一切工作正常,除非它不返回數組...任何建議將是偉大的! :-)遞歸函數不返回數組
$current_category = (int) $_GET['current_category'];
$cat_ids = array($current_category);
function getParentCatIds($current_category,$cat_ids){
$child = get_category($current_category);
$parent_id = $child->parent;
if($parent_id !== 0){
array_push($cat_ids, $parent_id);
getParentCatIds($parent_id,$cat_ids);
}else{
var_dump($cat_ids); // <--- this returns the right array
return $cat_ids; // <--- this returns NULL
}
}
if($current_category){
$cat_ids = getParentCatIds($current_category,$cat_ids);
var_dump($cat_ids); // <--- this returns NULL
}
只有當parent_id = 0時纔會返回,見第7行。所以這不會有問題。最高級別爲0.返回的是第12行。 –
我的錯誤!你是對的!返回是修復! :-) –