繁体   English   中英

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

[英]Get taxonomy terms without taxonomy name

我试图显示自定义帖子类型的分类术语(例如在常规帖子中使用<?php the_category( ' ' ); ?> )。 下面的代码有效,但是需要指定分类名称,是否可以仅使用Post ID?

<?php

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

提前致谢!

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

没有分类名称,您将无法获得自定义分类术语,但是上面的代码对您来说更短。

我找到了一种方法。 使用“ 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); } }

?> 

尝试这个。,

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

您必须使用分类法,否则分类法无法获取术语详细信息。

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM