admin管理员组文章数量:1392003
$args = array(
'post_type' => 'listing',
'order' => 'ASC',
'hide_empty' => false,
'parent' => 0,
);
$listCatTerms = get_terms( 'listing-category',$args);
if ( ! empty( $listCatTerms ) && ! is_wp_error( $listCatTerms ) ){
foreach ( $listCatTerms as $term ) {
echo '<li class="lp-wrap-cats" data-catid="'.$term->term_id.'">'.$catIcon.'<span class="lp-s-cat">'.$term->name.'</span></li>';
$defaultCats .='<li class="lp-wrap-cats" data-catid="'.$term->term_id.'">'.$catIcon.'<span class="lp-s-cat">'.$term->name.'</span></li>';
}
}
$args = array(
'post_type' => 'listing',
'order' => 'ASC',
'hide_empty' => false,
'parent' => 0,
);
$listCatTerms = get_terms( 'listing-category',$args);
if ( ! empty( $listCatTerms ) && ! is_wp_error( $listCatTerms ) ){
foreach ( $listCatTerms as $term ) {
echo '<li class="lp-wrap-cats" data-catid="'.$term->term_id.'">'.$catIcon.'<span class="lp-s-cat">'.$term->name.'</span></li>';
$defaultCats .='<li class="lp-wrap-cats" data-catid="'.$term->term_id.'">'.$catIcon.'<span class="lp-s-cat">'.$term->name.'</span></li>';
}
}
Share
Improve this question
asked Nov 22, 2017 at 10:16
Lovin NagiLovin Nagi
947 bronze badges
3 Answers
Reset to default 1Here you go:
$args = array(
'post_type' => 'listing',
'order' => 'ASC',
'hide_empty' => false,
'parent' => 0,
);
$listCatTerms = get_terms( 'listing-category',$args);
if ( ! empty( $listCatTerms ) && ! is_wp_error( $listCatTerms ) ){
echo "<ul>";
foreach ( $listCatTerms as $term ) {
echo '<li>'.$term->name.'</li>';
$child_terms = get_categories( array(
'parent' => $term->term_id,
'hide_empty' => 0 ));
//var_dump($child_terms);
echo "<ul>";
foreach($child_terms as $child_term){
echo '<li> -- '.$child_term->name.'</li>';
}
echo "</ul><br>";
}
echo "</ul>";
}
This should display the name of parent category and all its child category after.
You Can Try This Code
$getterms=get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => 0 )); foreach( $getterms as $parent_term ) { echo $parent_term->name; $getpchild=get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) ); foreach( $getpchild as $child_term ) { echo $child_term->name; } }
$args = array(
'post_type' => 'listing',
'order' => 'ASC',
'hide_empty' => false,
'parent' => 0,
);
$listCatTerms = get_terms( 'listing-category',$args);
if ( ! empty( $listCatTerms ) && ! is_wp_error( $listCatTerms ) ){
echo "<ul>";
foreach($listCatTerms as $listCatTerm) {
//parent categories
echo '<li>'.$listCatTerm->name.'</li>';
//get childern term ID
$term_children = get_term_children( $listCatTerm->term_id, 'listing-category' );
foreach ( $term_children as $child ) {
//Get all Term data from database by Term field and data.
$term = get_term_by( 'id', $child, 'listing-category' );
//childern categories of its parent category
echo '<li class="lp-wrap-cats">'.$term->name.'</li>';
}
}
echo "</ul>";
}
本文标签: How to display child categories of all parent category within a singe loop
版权声明:本文标题:How to display child categories of all parent category within a singe loop? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744695463a2620250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论