wordpress中的分类法术语列表

时间:2013-10-11 11:49:22

标签: wordpress custom-taxonomy

我需要在我的帖子(single.php)中使用分类法的数组术语。我正在尝试

<?php $term_list = wp_get_post_terms($post->ID, 'agencia', array("fields" => "names")); print_r($term_list); ?>

但是html out是:

Array ( [0] => Agencia 1 )

我想只播放“Agencia 1”,如果有2个词,则必须显示“Agencia 1,Agencia 2”

我做错了什么?

创建分类:

//注册自定义分类 function custom_taxonomy(){

$labels = array(
    'name'                       => _x( 'Agencias', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Agencia', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Genre', 'text_domain' ),
    'all_items'                  => __( 'All Genres', 'text_domain' ),
    'parent_item'                => __( 'Parent Genre', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Genre:', 'text_domain' ),
    'new_item_name'              => __( 'New Genre Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Genre', 'text_domain' ),
    'edit_item'                  => __( 'Edit Genre', 'text_domain' ),
    'update_item'                => __( 'Update Genre', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate genres with commas', 'text_domain' ),
    'search_items'               => __( 'Search genres', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove genres', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used genres', 'text_domain' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
);
register_taxonomy( 'agencia', 'post', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_taxonomy', 0 );

2 个答案:

答案 0 :(得分:0)

问题是您使用print_r转储$term_list的内容。 $term_list是一个数组,print_r会向您显示该数组及其结构。

尝试类似

的内容
foreach ($term_list as $term) {
    echo $term;
}

添加您需要的任何包装标签。

答案 1 :(得分:0)

我正在尝试一切。最后我在很长时间后在谷歌中找到了正确的答案。每个人的解决方案是:

<?php $terms = wp_get_post_terms($post->ID,'agencia');
$count = count($terms);
if ( $count > 0 ){
echo "<span>";
foreach ( $terms as $term ) {
echo $term->name . "<comma>, </comma>";
}
echo "</span>";} ?>

我创建了删除css的最后一个逗号:last-child

相关问题