如何从子类别中仅获取孙子类别

时间:2018-12-25 作者:user3244304

如何从子类别列表中仅获取孙子类别当前我正在使用下面的代码获取父类别的子类别。

<?php

$args = array(
\'orderby\' => \'name\',
\'parent\' => 72,
\'taxonomy\' => \'category\',
\'hide_empty\' => 0 ,
\'number\' => \'21\'
);
$categories =  get_categories(\'hide_empty=0&child_of=\'.$cat);
$content=\'\';

foreach ( $categories as $category ) {
echo \'<div class="col-md-4 inline-block padding-0" style="margin-bottom:10px;"><a href="\' . get_category_link( $category->term_id ) . \'">\'.    $category->name . \'</a></div>\';

}

?> 

1 个回复
SO网友:Cdorob

一种方法是在foreach中创建另一个get\\u类别($类别为$类别)

Example code

   //preparing an array to hold later info
    $grandchildren_ids = [];
    //getting the child categories of parent 72
    $args = array(
     \'orderby\' => \'name\',
     \'parent\' => 72,
     \'taxonomy\' => \'category\',
     \'hide_empty\' => 0
    );
    $categories =  get_categories($args);

    foreach ( $categories as $category ) {
    //setting up the args where the parents are the child categories
     $grandchildrenargs=array(
     \'parent\' => $category->term_id,
     \'taxonomy\' => \'category\',
     \'hide_empty\' => 0
     );
     $grandchildrencategories =  get_categories($grandchildrenargs);

      foreach ( $grandchildrencategories as $grandchildrencategory ) {
       //getting the grandchildren ids or whatever else is needed and populating the array
       $grandchildren_ids[] = $grandchildrencategory->term_id;
      }

     }
    var_dump($grandchildren_ids);

相关推荐

WP_DROPDOWN_CATEGORIES-如何在Widget中保存?

我想用wp_dropdown_categories 在自定义小部件中。所有内容都显示得很好,但由于某些原因,无法正确保存。这是form() 和update() 小部件的功能-我做错什么了吗?public function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( \'title\' => \'Classes by Category\' );&#x