• I have made a custom taxonomy called ‘item’, and make a list of terms of it with code:

    $args_list = array(
    	'taxonomy' => 'item', // Registered tax name
    	'show_count' => true,
    	'hierarchical' => true,
    'hide_empty' => '0',
    'title_li' => '',
    	'echo' => '0',
    );
    echo wp_list_categories($args_list);

    Taxonomy terms are like:
    Country
    – USA
    – Canda
    Planet
    – earth
    – mars

    Now, I trying to get list of child terms of current term in another place of template, but can’t get current term ID. How can I get it? Thanx

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter demarket

    (@demarket)

    thanx very much, your code working fine, but one problem for me.

    This code returns an unordered list of all child terms, but my structure has many levels, and I want now to display only one child level of current page, something like ‘depth=1’ in wp_list_categories.

    How may i modify this code? Woul be very pleasant for u!

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this:

    $term_slug = get_query_var( 'term' );
      $taxonomyName = get_query_var( 'taxonomy' );
      $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
      $termchildren = get_term_children( $current_term->term_id, $taxonomyName);
      if($termchildren && ( !is_wp_error( $termchildren ))){
        echo '<ul>';
        foreach ($termchildren as $child) {
          $term = get_term_by( 'id', $child, $taxonomyName );
          if($current_term->term_id == $term->parent){
            echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
          }
        }
        echo '</ul>';
      }

    Or this 🙂

    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
    $args_list = array(
        'taxonomy' => 'item', // or use $taxonomyName for all taxonomies
        'show_count' => true,
        'hierarchical' => true,
        'child_of' => $current_term->term_id,
        'hide_empty' => '0',
        'title_li' => '',
        'echo' => '0',
    );
    echo wp_list_categories($args_list);

    to avoid the message No categories if there is no child categories, you can use after 3rd string ($current_term…) keesiemeijer’s condition code 🙂

    $termchildren = get_term_children( $current_term->term_id, $taxonomyName);
    if($termchildren && ( !is_wp_error( $termchildren ))) {
     // whole section with $args_list and wp_list_categories($args_list) here;
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to get term id of custom taxonomy’ is closed to new replies.