Created
September 5, 2013 19:32
-
-
Save brycejacobson/6454994 to your computer and use it in GitHub Desktop.
WordPress get child terms of current taxonomy page and display them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$taxonomy_name = get_queried_object()->name; // 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 '<tr>'; | |
foreach ( $termchildren as $child ) { | |
$term = get_term_by( 'id', $child, $taxonomy_name ); | |
echo '<td><a href="' . get_term_link( $term->name, $taxonomy_name ) . '">' . $term->name . '</a></td>'; | |
} | |
echo '</tr>'; |
Thanks @whafes
Purrfect, thank you!
Generally, this works for me pretty well.
<?php
$parent = get_queried_object();
$children = get_term_children($parent->term_id, $parent->taxonomy);
if (!empty($children)) {
echo '<ul>';
foreach ($children as $child) {
$child_term = get_term_by('id', $child, $parent->taxonomy);
echo '<li><a href="'.get_term_link($child_term, $parent->taxonomy).'">'.$child_term->name.'</a></li>';
}
echo '</ul>';
}
?>
However, if sorting child terms is important to you then you can go through a different route as well.
<?php
$parent = get_queried_object();
$children = get_terms(array(
'taxonomy' => $parent->taxonomy,
'parent' => $parent->term_id,
'orderby' => 'slug',
'order' => 'ASC',
'hide_empty' => true));
if (!empty($children)) {
echo '<ul>';
foreach ($children as $child) {
echo '<li><a href="'.get_term_link($child).'">'.$child->name.'</a></li>';
}
echo '</ul>';
}
?>
Hope this was helpful to some. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you will get the name of the taxonomy wich won't work on get_term_children()
$taxonomy_name = get_queried_object()->name;
just replace it with
$taxonomy_name = get_queried_object()->taxonomy;