Skip to content

Instantly share code, notes, and snippets.

@brycejacobson
Created September 5, 2013 19:32
Show Gist options
  • Save brycejacobson/6454994 to your computer and use it in GitHub Desktop.
Save brycejacobson/6454994 to your computer and use it in GitHub Desktop.
WordPress get child terms of current taxonomy page and display them.
<?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>';
@develanet
Copy link

Purrfect, thank you!

@iftekharbhuiyan
Copy link

iftekharbhuiyan commented Nov 13, 2021

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