代码之家  ›  专栏  ›  技术社区  ›  Avishay28

使用get_categories()获取多个类别的子类别

  •  1
  • Avishay28  · 技术社区  · 10 年前

    有没有办法获得几个类别的所有子类别?比如:

    get_categories( array( 'child_of'=>array(10,3,8) );
    
    1 回复  |  直到 10 年前
        1
  •  4
  •   Felipe Elia    10 年前

    这是不可能的,wordpress只接受所有获取类别子项的函数中的一个整数。你必须把他们的孩子分开:

    $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
    }
    

    希望有帮助!