get sub categories of several categories with get_categories()

落爺英雄遲暮 提交于 2021-01-28 20:06:46

问题


Is there a way to get all sub categories of several categories? something like:

get_categories( array( 'child_of'=>array(10,3,8) );

回答1:


It's not possible, wordpress just accepts one integer in all it's functions that get categories children. You'll have to get their children separately:

$terms = array();
$taxonomy = 'category';
$parents = array(10, 3, 8);
foreach ($parents as $parent) {
    $terms = array_merge($terms, get_categories(array('child_of'=> $parent)));
}

foreach ($terms as $term) {
    // your code here
}

Hope it helps!



来源:https://stackoverflow.com/questions/35825561/get-sub-categories-of-several-categories-with-get-categories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!