Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
what is stored in 'maingenre' exactly? include expects an ID. –  Milo Nov 11 '12 at 23:49
    
As I already said, 'maingenre' stores the id of one genre. –  René Schubert Nov 13 '12 at 8:58
add comment

1 Answer 1

up vote 0 down vote accepted

Always check the codex - get_terms( $taxonomies, $args ) - your code should read:

$genre = get_terms( 'genre', array(
    'include' => get_post_meta( $post->ID, 'maingenre', true ),
));
share|improve this answer
    
Thank you, it worked. Seems like I mixed up the parameters with the ones I found for a different function in the codex. –  René Schubert Dec 2 '12 at 20:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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