Reputation: 984
I'm trying to output only 1st child level (not parent or grandchildren) custom taxonomies in a loop of some sort, so i can add custom fields / thumbnails to it.
I created a hierarchical custom post type called 'product-type'
And there are a few levels of custom taxonomies to it.
Level 1 - Snacks
Level 2 - Chocolates (just showing 1 for this example)
Level 3 - Milk Chocolates, Dark Chocolates ..and so on.
on the parent taxonomy page, i've been able to successfully list all the existing Level 2 taxonomies with the following code:
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
wp_list_categories('taxonomy=product-type&depth=1&show_count=0
&title_li=&child_of=' . $term->term_id);
} else {
wp_list_categories('taxonomy=product-type&show_count=0
&title_li=&child_of=' . $term->parent);
}
?>
the output looks something like this:
<li class="cat-item cat-item-7">
<a href="..." title="...">Chocolates</a> (14)
</li>
My question is, how do i edit the php code above such that i can output (echo?) only only 1st child level (not parent or grandchildren) taxonomy in some sort of a loop where i can insert divs or with custom fields?
Upvotes: 0
Views: 884
Reputation: 191
Try this on for size.
https://codex.wordpress.org/Function_Reference/get_terms
<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
if ($term->parent == 0) {
$terms = get_terms( 'product-type', 'child_of='.$term->term_id );
} else {
$terms = get_terms( 'product-type', 'child_of='.$term->parent );
}
foreach($terms as $term) {
// Your custom HTML
}
?>
Upvotes: 1