如何显示自定义邮寄类型的自定义分类

时间:2013-09-06 作者:luckyrajiv

我已经创建了名为“地点”的自定义帖子类型

为此,我创建了名为“音乐”的自定义分类类别。

代码如下:

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 );
}
现在,我想在中显示类别single-places.php.

如何显示类别“音乐”。

2 个回复
最合适的回答,由SO网友:Anjum 整理而成

您可以使用wp_get_post_terms(); 内部环路

<?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 );
} ?>

SO网友:cybmeta

尽管@Anjum的答案非常有效,但也可以使用get_the_terms(), 使用以下函数获取术语wp_get_post_terms()get_object_term_cache(), 视情况而定。所以使用get_the_terms() 如果您不需要传递额外的参数,可以更合适,因为它是这样的。

<?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 );
}
?>
参考文献:

  1. get_the_terms()
  2. wp_get_post_terms()

结束

相关推荐