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 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, I want to display the categories in single-places.php.

How can I display the categories "Music".

share|improve this question

2 Answers 2

up vote 1 down vote accepted

you can get terms in single post type template by using wp_get_post_terms(); inside loop

<?php $terms = wp_get_post_terms($post->ID, 'music');
if ($terms) {
    $out = array();
    foreach ($terms as $term) {
        $out[] = '<a class="' .$term->slug .'" href="' .get_term_link( $term->slug, 'music') .'">' .$term->name .'</a>';
    }
    echo join( ', ', $out );
} ?>
share|improve this answer
    
thanks. got it. –  luckyrajiv Sep 6 '13 at 18:26
    
After the query, i get Music: Rock, Jazz, House, .... I want to add icons in front of the Rock, Jazz etc, can you create classes for each value like rock, jazz so that i can add icons in front of the categories –  luckyrajiv Sep 6 '13 at 18:27
1  
@luckyrajiv ok mark this question as solved by accept this answer. –  Anjum Sep 6 '13 at 18:29
    
can you please create classes for each categories and give so that i can add icons –  luckyrajiv Sep 6 '13 at 18:30
    
ok edited my answer and added term slug as css class now you can style each term as you wish. –  Anjum Sep 6 '13 at 18:32

Although the answer of @Anjum works perfectly, it can be done also using get_the_terms(), a function that get the terms using wp_get_post_terms() or get_object_term_cache(), depend of the situation. So using get_the_terms() can be more appropiate if you don't need to pass extra arguments, as it is the case.

<?php
$terms = get_the_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 );
}
?>

References:

  1. get_the_terms()
  2. wp_get_post_terms()
share|improve this answer
    
its repeating each category into next categories –  luckyrajiv Sep 7 '13 at 16:19
    
its 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
    
In your question you say "I created Custom Taxonomy Category called "Music"......How can I display the categories "Music"?" and that is what this code does, and it does using a better function that the accepted answer (I think, read the reasons in my answer). If your question has changed, please edit it or start new one. –  cybmeta Sep 8 '13 at 8:16
    
Can you post the code that your are currently using? –  cybmeta Sep 8 '13 at 8:23

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.