Display Child taxonomy link on taxonomy archive page

Author: Aleš Sýkora, 12. 3. 2021

Need to display a list of currrent taxonomy archive child taxonomies with links? You can use the wp_list_categories or get_categories.

First approach -> using pre styled output with wp_list_categories.


<?php 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
if ($term->parent == 0) {  
$args = array(
		'taxonomy' => 'your-custom-tax-name',
  		'depth'    => 1,
  		'show_count' => 0,
  		'title_li' => '',
  		'child_of' => $term->term_id
	);
wp_list_categories($args);
} else {
  $args = array(
		'taxonomy' => 'your-custom-tax-name',
  		'depth'    => 1,
  		'show_count' => 0,
  		'title_li' => '',
  		'child_of' => $term->parent
	);
wp_list_categories($args);
}
?>

Second approach -> using custom html output with get_categories.

<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
if ($term->parent == 0) {  
  $categories = get_categories( array(
   'taxonomy' => 'zarizeni',
  		'depth'    => 1,
  		'show_count' => 0,
  		'title_li' => '',
  		'child_of' => $term->term_id
) ); 
} else {
  $categories = get_categories( array(
		'taxonomy' => 'zarizeni',
  		'depth'    => 1,
  		'show_count' => 0,
  		'title_li' => '',
  		'child_of' => $term->parent
	)); }
 
foreach ( $categories as $category ) {
    printf( '<a href="%1$s">%2$s</a><br />',
        esc_url( get_category_link( $category->term_id ) ),
        esc_html( $category->name )
    );
}

?>