1

I've got a custom post type with multiple custom taxonomies + metaboxes. I've used multiple approaches from the codex and other on line resources but none seem to work. I basically want to display the current posts taxonomy values below the title on each custom post's page. This is what I have so far...

In functions.php:

function discography_album_label() {
    $labels = array(
        'name'              => _x( 'Labels', 'taxonomy general name' ),
        'singular_name'     => _x( 'Label', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Labels' ),
        'all_items'         => __( 'All Labels' ),
        'parent_item'       => __( 'Parent Label' ),
        'parent_item_colon' => __( 'Parent Label:' ),
        'edit_item'         => __( 'Edit Label' ), 
        'update_item'       => __( 'Update Label' ),
        'add_new_item'      => __( 'Add New Label' ),
        'new_item_name'     => __( 'New Label' ),
        'menu_name'         => __( 'Labels' ),
    );
    $args = array(
        'labels' => $labels,
        'hierarchical' => false,
        'rewrite' => array( 'slug' => 'label' ),
    );
    register_taxonomy( 'discography_album_label', 'discography', $args );
}
add_action( 'init', 'discography_album_label', 0 );

In single-discography.php

$genreTax = get_taxonomies( '', 'names' );
$terms = wp_get_post_terms($post->ID, $genreTax,  array("fields" => "names", "orderby" => "type"));

<p><?php print_r($terms);?></p>

However all this is outputting is:

Array ( [0] => Ska [1] => Reggae [2] => Record Label ) 

...which doesn't really help as it's the values for the "Labels" and and the "Genres" taxonomies (with no way of differentiating which is from which). How can I get just the value for one of these taxonomies?

  • try get_the_terms( $post->ID, 'discography_album_label' ) – karpstrucking Jan 9 '15 at 19:58
  • After trying: $albumGenres = get_the_terms( $post->ID, 'discography_album_label' ); <?php print_r($albumGenres);?> I see: Array ( [17] => stdClass Object ( [term_id] => 17 [name] => Record Label [slug] => record-label [term_group] => 0 [term_taxonomy_id] => 17 [taxonomy] => discography_album_label [description] => [parent] => 0 [count] => 1 [object_id] => 43 [filter] => raw ) ) How can I just get the name value? I tried <?php print_r($albumGenres->name);?> but nothing – Brad Adams Jan 9 '15 at 20:12
  • you need to loop through the array of objects returned by get_the_terms() and then grab the name property. foreach ( $albumGenres as $albumGenre ) { echo $albunGenre->name; } – karpstrucking Jan 9 '15 at 20:18
  • Ahh ofcourse... thanks, post this as the answer! :) – Brad Adams Jan 9 '15 at 20:23
2
$albumGenres = get_the_terms( $post->ID, 'discography_album_label' );
foreach ( $albumGenres as $albumGenre ) {
  echo $albumGenre->name; // or whatever value
}

Your Answer

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

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