是否显示自定义帖子类型自定义分类值?

时间:2015-01-09 作者:Brad Adams

我有一个带有多个自定义分类法+元数据库的自定义帖子类型。我使用了来自codex和其他在线资源的多种方法,但似乎都不起作用。我基本上希望在每个自定义帖子页面的标题下显示当前帖子的分类值。这就是我目前所拥有的。。。

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>
然而,所有这些输出都是:

Array ( [0] => Ska [1] => Reggae [2] => Record Label ) 
。。。这并没有真正的帮助,因为这是“标签”的值and 以及“流派”分类法(无法区分哪个是哪个)。我如何才能获得其中一种分类法的值?

1 个回复
最合适的回答,由SO网友:karpstrucking 整理而成
$albumGenres = get_the_terms( $post->ID, \'discography_album_label\' );
foreach ( $albumGenres as $albumGenre ) {
  echo $albumGenre->name; // or whatever value
}
结束