在标题中显示分类术语而不是类别

Displaying taxonomy term instead of category in title

首先,我真的很讨厌 Wordpress。

我有以下代码显示搜索结果中每个 post 的类别:

<?php 
   $category = get_the_category();
   if ($category ) {
        echo '<a href="' . get_category_link( $category[0]->term_id ) . '" title="' . $category[0]->name . '" ' . '>' . $category[0]->name.'</a>';
        if(isset($category[1])){
           echo ' ...';
        }
   }
?>

我想要做的是用 post 碰巧拥有的任何分类法的术语名称替换类别。我已经看过一堆类似的问题,但我无法让它工作。

我想这与此有关:https://codex.wordpress.org/Function_Reference/get_term 但由于我的 PHP 技能有限,我卡住了。

非常感谢任何帮助!

如果在为每个 post 显示 terms/term 之后,您可以使用 get_the_terms。

// Get taxonomy terms specifc to current post
$terms =  get_the_terms( $post->ID , 'your-taxonomy-name-here' );

foreach ( $terms as $term ) {
    echo '&nbsp;<a href="' .esc_url( home_url( '/' ) ). $term->slug . '" title="' . $term->name . '" ' . '>' . $term->name .'</a> &nbsp;';
}

您需要做的就是将 'your-taxonomy-name-here' 替换为您的分类法名称,并根据您的喜好设置输出样式。