获取没有分类名称的分类术语

时间:2017-04-18 18:26:50

标签: wordpress

我正在尝试显示自定义帖子类型的分类术语(例如在常规帖子中使用<?php the_category( ' ' ); ?>)。下面的代码有效但需要指定分类名称,有没有办法只使用帖子ID?

<?php

   $terms = get_the_terms( $post->ID , 'taxonomy_name' );
   foreach ( $terms as $term ) {
     echo $term->name;
   }
?>

提前致谢!

3 个答案:

答案 0 :(得分:0)

<?php print the_terms( $post->ID, 'taxonomy_name' , ' ' ); ?>

如果没有分类名称,您将无法获得自定义分类条款,但上述代码对您来说更短。

答案 1 :(得分:0)

我找到了办法。使用“get_post_taxonomies”并选择阵列巫婆[1]

<?php  

$cat = get_post_taxonomies($post);
$terms = get_the_terms( $post->ID , $cat[1] );

// Loop
if ( $terms != null ){

   foreach( $terms as $term ) {
   $term_link = get_term_link( $term, $cat[1] );

   // Print the name and URL
   echo '<a href="' . $term_link . '">' . $term->name . '</a>';

   unset($term); } }

?> 

答案 2 :(得分:0)

试试这个。,

 $term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "names"));
    print_r($term_list);

您必须使用不带分类标准的分类法才能获得术语详细信息。

谢谢!

相关问题