自定义 post 类型中的 Wordpress 列表类别

Wordpress list category in custom post type

我在下面有一个查询,我只想列出分配给当前 post 我正在查看的那些类别。

目前,它列出了我的自定义 post 类型的所有类别。 是否可以只列出个人 post 的那些? post 类型称为 'resource',附加到此 post 类型的类别称为 'resource-category'。

 <?php
      $taxonomy = 'resource-category';
      $tax_terms = get_terms($taxonomy);
      ?>
   <?php
      foreach ($tax_terms as $tax_term) {
      echo '' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a>&nbsp;&nbsp;&nbsp; ';
      }
?>

您可以使用 wp_get_post_terms:

<?php 

$taxonomy = 'resource-category';
$tax_terms = wp_get_post_terms($post->ID, $taxonomy, array("fields" => "all"));

foreach ($tax_terms as $tax_term) {
  echo '' . '<a href="' . esc_attr(get_term_link($tax_term->term_id, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a>&nbsp;&nbsp;&nbsp; ';
}

?>