在自定义帖子类型中显示当前分类术语

时间:2013-03-01 作者:gil hamer

这应该很简单,但我在网上找不到答案。我找到的所有答案都很接近,但并不完全符合我的需要。我需要的是只显示我所在的自定义帖子类型的当前术语。not all the terms just one! (相关人员)

这是我正在使用的,但它显示了所有对我不利的术语:

<?php
$taxonomy = \'genre\';
$queried_term = get_query_var($taxonomy);
$terms = get_terms($taxonomy, \'slug=\'.$queried_term);
if ($terms) {
  foreach($terms as $term) {
    echo $term->name;
  }
}
?>
请记住,我想在我的单一帖子类型模板中显示它,有人能建议吗?谢谢

4 个回复
最合适的回答,由SO网友:gil hamer 整理而成

好了,我终于在这里找到了我需要的:How to get current term in my custom taxonomy in WordPress?

底部的最后一次更新由@user3208提供:

<?php   // Get terms for post
 $terms = get_the_terms( $post->ID , \'oil\' );
 // Loop over each item since it\'s an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
 // Print the name method from $term which is an OBJECT
 print $term->slug ;
 // Get rid of the other data stored in the object, since it\'s not needed
 unset($term);
} } ?>
这解决了我的问题!谢谢

SO网友:RRikesh

您应该使用wp_get_post_terms 相反

$terms = wp_get_post_terms( $post_id, $taxonomy, $args );
get_terms 将为您提供分类中存在的所有术语。

UPDATE:

global $post;
$terms = wp_get_post_terms( $post->ID, \'genre\');
print_r($terms); #displays the output

SO网友:Duncan Micahel-MacGregor

以user3208代码为例,我添加了一些代码,将URL添加到术语中。希望这能帮助别人。

<?php   // Get terms for post
$terms = get_the_terms( $post->ID , \'oil\' );
// Loop over each item since it\'s an array
if ( $terms != null ){
foreach( $terms as $term ) {
$term_link = get_term_link( $term, \'oil\' );
 // Print the name and URL
echo \'<a href="\' . $term_link . \'">\' . $term->name . \'</a>\';
// Get rid of the other data stored in the object, since it\'s not needed
unset($term); } } ?>

SO网友:Anderson Narciso
<?php echo get_the_term_list( $post->ID, \'yourtaxonomy\', \'\', \', \' ); ?>
结束

相关推荐

encoded search terms with %20

<form class=\"search-form\" action=\"<?php echo home_url( \'/suche/\' ); ?>\" method=\"get\"> <input type=\"text\" name=\"s\" class=\"s\" value=\"<?php the_search_query(); ?>\" /> 这是我的searchform.php 样板你知道为什么在搜索例如“