$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - how to get only grandchild category from a child category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - how to get only grandchild category from a child category

matteradmin11PV0评论

how to get only grandchild category from a child category list currently i am using the code bellow for get child category of parent.

<?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>';

}

?> 

how to get only grandchild category from a child category list currently i am using the code bellow for get child category of parent.

<?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>';

}

?> 
Share Improve this question edited Dec 25, 2018 at 10:57 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Dec 25, 2018 at 10:00 user3244304user3244304 1
Add a comment  | 

1 Answer 1

Reset to default 0

One way you could do it is to make another get_categories inside the foreach ( $categories as $category )

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);
Post a comment

comment list (0)

  1. No comments so far