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!