I have created Custom Post type called "places"

For that I created Custom Taxonomy Category called "Music".

Here is the code:

add_action( 'init', 'register_taxonomy_music' );

function register_taxonomy_music() {

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

$args = array( 
    'labels' => $labels,
    'public' => true,
    'show_in_nav_menus' => true,
    'show_ui' => true,
    'show_tagcloud' => true,
    'show_admin_column' => false,
    'hierarchical' => true,

    'rewrite' => true,
    'query_var' => true

register_taxonomy( 'music', array('places'), $args );

Now, To display the categories in single-places.php.

I am calling this

<?php $terms = wp_get_post_terms($post->ID, 'music');
if ($terms) {
    foreach ($terms as $term) {
        $out[] = '<a class="' .$term->slug .'" href="' .get_term_link( $term->slug, 'music') .'">' .$term->name .'</a>';
    echo join( ', ', $out );
} ?>

It is showing the results perfectly.

and with this if i am calling additional categories like music, genre, pub, location etc by the above code.

First Category is proper, Second Category shows First plus Second third Category is showing First+second+Third

Please Help

  • you never initialize $out, so if you run this code multiple times, you'll just keep appending results onto $out. also fyi, when you have a term object, you can pass that to get_term_link without having to set the taxonomy: get_term_link( $term ) – Milo Sep 7 '13 at 15:57
  • can you tell me what addition or changes i need to do for making it correct. – luckyrajiv Sep 7 '13 at 16:11
  • its showing like this (1) Place: Chicago (2) Music: Chicago, Rock, Jazz (3) Food: Chicago, Rock, Jazz , Chinese, Thai, Italian It is first showing previous fields then that category everytime – luckyrajiv Sep 7 '13 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.