I’m trying to list the terms of a custom taxonomy (‘genre’) in the overview table in the admin area for a custom post type (‘station’). To get this, I tried the following code in my theme’s functions.php file:
register_taxonomy('genre', 'station', array(
'labels' => array(
'name' => _x('Genres', 'taxonomy general name', 'loudfm_theme'),
'singular_name' => _x('Genre', 'taxonomy singular name', 'loudfm_theme'),
'search_items' => __('Search Genres', 'loudfm_theme'),
'popular_items' => __('Popular Genres', 'loudfm_theme'),
'all_items' => __('All Genres', 'loudfm_theme'),
'parent_item' => __('Parent Genre', 'loudfm_theme'),
'parent_item_colon' => __('Parent Genre:', 'loudfm_theme'),
'edit_item' => __('Edit Genre', 'loudfm_theme'),
'update_item' => __('Update Genre', 'loudfm_theme'),
'add_new_item' => __('Add New Genre', 'loudfm_theme'),
'new_item_name' => __('New Genre Name', 'loudfm_theme')
),
'hierarchical' => true
));
add_filter('manage_station_posts_columns', 'station_edit_columns');
add_action('manage_posts_custom_column', 'station_custom_columns');
function station_edit_columns($columns){
$columns = array(
'cb' => '<input type="checkbox"/>',
'title' => __('Station Title', 'loudfm_theme'),
'description' => __('Description', 'loudfm_theme'),
'genre' => __('Genre', 'loudfm_theme')
);
return $columns;
}
function station_custom_columns($column){
global $post;
switch ($column) {
case 'description':
the_excerpt();
break;
case 'genre':
$genre = get_terms(array(
'taxonomy' => 'genre',
'include' => get_post_meta($post->ID, 'maingenre', true)
));
echo '<a href="' . get_term_link($genre[0]) . '" title="' . sprintf(__('View all stations with the genre %s', 'loudfm_theme'), $genre[0]->name) . '">' . $genre[0]->name . '</a>';
}
}
In my case I don’t simply want to list all selected genres of a station, but only the main genre whose id is stored in the field ‘maingenre’ of the custom post type. That’s why I’m using the get_terms
function with the ‘include’ parameter. But sorrily I get an ‘Invalid taxonomy’ error back (as WP_Error object). Using get_the_term_list
works without errors, but with this function I am not able to display only the main genre.
Can anyone tell me, why get_terms
is not working here and how I can get it to work? Thank you!
Always check the codex –
get_terms( $taxonomies, $args )
– your code should read: