I want to output the sub category details of a taxonomy
$getTerms = get_terms($taxonomy, $args);
print_r($getTerms);
When i print the above out it returns the object. But theres no value difference between parent and sub categories. The first is a parent taxonomy and the second is a sub-category.
[1] => stdClass Object
(
[term_id] => 23
[name] => Corporate teams
[slug] => corporate-teams
[term_group] => 0
[term_taxonomy_id] => 23
[taxonomy] => team_names
[description] => Description of corporate team
[parent] => 0
[count] => 0
[image_id] => 0
)
[3] => stdClass Object
(
[term_id] => 25
[name] => Team name 1
[slug] => team-name-1
[term_group] => 0
[term_taxonomy_id] => 25
[taxonomy] => team_names
[description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nec pellentesque sapien.
[parent] => 22
[count] => 1
[image_id] => 90
)
How would i query part of this object (likely as a foreach or as a WP_Query) so it will return the sub categories and their relavant values (title, image,etc)? Is the 'get_terms()' function the right way to go for this?
I want to output the sub category details of a taxonomy
$getTerms = get_terms($taxonomy, $args);
print_r($getTerms);
When i print the above out it returns the object. But theres no value difference between parent and sub categories. The first is a parent taxonomy and the second is a sub-category.
[1] => stdClass Object
(
[term_id] => 23
[name] => Corporate teams
[slug] => corporate-teams
[term_group] => 0
[term_taxonomy_id] => 23
[taxonomy] => team_names
[description] => Description of corporate team
[parent] => 0
[count] => 0
[image_id] => 0
)
[3] => stdClass Object
(
[term_id] => 25
[name] => Team name 1
[slug] => team-name-1
[term_group] => 0
[term_taxonomy_id] => 25
[taxonomy] => team_names
[description] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nec pellentesque sapien.
[parent] => 22
[count] => 1
[image_id] => 90
)
How would i query part of this object (likely as a foreach or as a WP_Query) so it will return the sub categories and their relavant values (title, image,etc)? Is the 'get_terms()' function the right way to go for this?
You can use the parent
argument in get_terms()
to get direct children of a term.
You can use the child_of
argument to get all descendents of a term.
Something like this.
$parent_id = 32;
$args = array(
'parent' => $parent_id
);
$terms = get_terms( $taxonomy, $args );
print_r( $terms );
If you want to programmatically get parent terms and within the loop get sub-categories, you could do something like this.
$taxonomy = 'your_tax';
$args = array(
'parent' => 0 // to get only parent terms
);
$terms = get_terms( $taxonomy, $args );
foreach( $terms as $term ) {
$children = get_terms( $taxonomy, array(
'parent' => $term->term_id;
) );
print_r( $children );
}
http://codex.wordpress/Function_Reference/get_terms
Since wordpress 4.5.0, taxonomies should be passed through 'taxonomy' argument in $args array :
$parent_id = 32;
$args = array(
'taxonomy' => $taxonomy,
'parent' => $parent_id
);
$terms = get_terms( $args );
print_r( $terms );
get_terms in official doc
<div class="categories-item">
<?php
$wcatTerms = get_terms('service_cat', array('hide_empty' => 0, 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<button class="accordion"><?php echo $wcatTerm->name; ?></button>
<div class="panel">
<?php
$wcatTerms1 = get_terms('service_cat', array('hide_empty' => 0, 'parent' =>$wcatTerm->term_id));
foreach($wcatTerms1 as $wcatTerm1) :
?>
<a href="<?php echo get_term_link( $wcatTerm1->slug, $wcatTerm1->taxonomy ); ?>"><?php echo $wcatTerm1->name; ?></a>
<?php endforeach ; ?>
</div>
<?php endforeach ; ?>
<!-- <button class="accordion">Section 3</button>
<div class="panel">
</div> -->
</div>