Aleš Sýkora / March 12, 2021 / 0 comments

Display Child taxonomy link on taxonomy archive page

Post summary: 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. Second approach -> using custom html output with get_categories.

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 )
    );
}

?>

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts