admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0One 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);
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
1 Answer
Reset to default 0One 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);
本文标签: categorieshow to get only grandchild category from a child category
版权声明:本文标题:categories - how to get only grandchild category from a child category 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749069197a2311308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论