私はWorpressのすべてのカテゴリの親IDをカテゴリ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行目です。 –
私の間違い!あなたはとても正しいです!リターンは修正でした! :-) –