Aleš Sýkora wrote

Display Child taxonomy link on taxonomy archive page

12. 3. 2021 in

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

?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

More from category
Aleš Sýkora wrote
WooCommerce category clustered by child taxonomies in WordPress
6. 2. 2021 in
If you need to display your parent product category sorted by child taxonomies in clusters – for each child taxonomy it owns row – for example: Breakfasts Category Archive – Parent category Vegetarian – child category Product 1 Product 2 Vegan – child category Product 1 Product 2 Meat lovers – child category Product 1 […]
Read More
Aleš Sýkora wrote
How to set scroll to the top with jQuery on click button
14. 9. 2021 in
Change the #submitButton to your button ID.
Read More
Aleš Sýkora wrote
Translate labels in Iconic Delivery Slots
21. 1. 2021 in
If you need to change labels of fields added by Iconic Delivery Slots to WooCommerce checkout, use this snippet:
Read More
magnifiercross linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram