I would like to have a couple of (automatically generated) taxonomy pages created by term name, term description and a listing of only the next level of subterms. (No parents and no grandchildrens)
I have set up a CPT named "Products", and a custom hierarchical taxonomy called "Structure". The taxonomy has the hierarchical terms
level1a
level1b
-level2a
-level2b
--level3a
--level4a
level1c
...
I'm trying to achieve to get taxonomy pages of /level1a/ and /level1b/level2a/ and so on.
On each of these pages there should be the term-title, the term-description and a list of direct child-terms of the current term. (No grandchildren).
In functions.php I have created the CPT with the
'rewrite' => array('slug' => 'products', 'hierarchical' => true, 'with_front' => false),
option and the custom taxonomy with the
'rewrite' => array( 'slug' => 'product', 'hierarchical' => true, 'with_front' => false),
option.
I am now struggling with the correct query in taxonomy.php to get the direct children only.
The closest i found is
<?php
$taxonomy_name = get_queried_object()->taxonomy; // Get the name of the taxonomy
$term_id = get_queried_object_id(); // Get the id of the taxonomy
$termchildren = get_term_children( $term_id, $taxonomy_name ); // Get the children of said taxonomy
// Display the children
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $term->name, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?>
But this is showing all children and grandchildren
Thanks for your time.
get_term_children
it looks like a helper method. Perhaps it would be better to skip the middleman and go direct toWP_Term_Query
?