0

I am trying to display a list of terms for a custom taxonomy and am having a bit of trouble. The custom tax is for a Custom Post Type called project.

What I want to do is list a Parent term and it's children, such as:

  • Painting
    • 2013
    • 2012
    • 2011

When you get to an archive of a child term, the same list would appear, showing the parent term.

I have attempted to use wp_list_categories() and it has potential but I am not sure how to manipulate it for these purposes, meaning that when the archive is for a child term, the parent term and all other child terms are not displayed.

The code I have in the template isn't working at all, and it comes from a WP Codex example. Here is the template code:

  global $wp_query;
  $term = $wp_query->get_queried_object();
  $title = $term->name;                     
  $termID = $term->term_id;
  $taxonomy_name = $term->taxonomy;
  $termChildren = get_term_children( $termID, $taxonomy_name );

  echo $title;
  echo $termChildren;

  echo '<ul>';

  foreach ( $termChildren as $termChild ) {
    $term2 = get_term_by( 'id', $termChild, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $term2->name, $taxonomy_name ) . '">' . $term2->name . '</a></li>';
  }

  echo '</ul>';

This does not render a list, and breaks everything underneath the foreach statement.

Just for good measure, here is the Custom Tax code in case I did anything stupid there:

add_action( 'init', 'register_taxonomy_mediums' );

function register_taxonomy_mediums() {

    $labels = array( 
        'name' => _x( 'Mediums', 'mediums' ),
        'singular_name' => _x( 'Medium', 'mediums' ),
        'search_items' => _x( 'Search Mediums', 'mediums' ),
        'popular_items' => _x( 'Popular Mediums', 'mediums' ),
        'all_items' => _x( 'All Mediums', 'mediums' ),
        'parent_item' => _x( 'Parent Medium', 'mediums' ),
        'parent_item_colon' => _x( 'Parent Medium:', 'mediums' ),
        'edit_item' => _x( 'Edit Medium', 'mediums' ),
        'update_item' => _x( 'Update Medium', 'mediums' ),
        'add_new_item' => _x( 'Add New Medium', 'mediums' ),
        'new_item_name' => _x( 'New Medium', 'mediums' ),
        'separate_items_with_commas' => _x( 'Separate mediums with commas', 'mediums' ),
        'add_or_remove_items' => _x( 'Add or remove mediums', 'mediums' ),
        'choose_from_most_used' => _x( 'Choose from the most used mediums', 'mediums' ),
        'menu_name' => _x( 'Mediums', 'mediums' ),
    );

    $args = array( 
        'labels' => $labels,
        'public' => true,
        'show_in_nav_menus' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'show_admin_column' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'rewrite' => true,
        'query_var' => true
    );

    register_taxonomy( 'mediums', array('project'), $args );
}

Any help would be great. Thanks!

1 Answer 1

1

Your code works perfectly in a custom taxonomy template file.

CASE 1

You're trying to use this code in a single page or something else and it doesn't work because the object returned from get_queried_object() isn't a taxonomy. The function get_queried_object() return the currently-queried object. So, if you need a Taxonomy Object you have to call it in a taxonomy archive page.

CASE 2

You're using this code in a taxonomy archive page. If you need to display the same list for parents and childrens you have to check if the current term is a children or a parent, because you have to call get_term_children() with the parent id as argument.

If you call get_term_children() with the current term ID on the children you'll probably get an empty list.

So check $term->parent value. This variable outputs the ID of the parent taxonomy. So if you are on the parent taxonomy, then $term->parent will echo 0 and you can call get_term_children() with the $term->term_id value. If you are on a children you have to call get_term_children() with the $term->parent value.

3
  • Case 2 is the situation. I'm basically using an archive as a gallery. I tried wrapping get_term_children() and the foreach statement together in a conditional if($term->parent == 0), however it still does not output anything on a parent page...
    – Baerkins
    Commented Nov 18, 2013 at 22:24
  • Is the $termChildren array properly populated? Have you added terms as children in backend? It is weird I copied and pasted your code in a fresh wp install, added some terms and it worked! Commented Nov 18, 2013 at 23:41
  • Interesting, maybe I will have to try that out. When I run print_r on get_term_children, it spits only out IDs for the children. Backend admin does have children setup properly: cl.ly/image/1n1m1w1E210c, is that what you mean?
    – Baerkins
    Commented Nov 19, 2013 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.