我正在使用此代码来显示层次结构的自定义帖子类别,一切都工作到现在很好但我想要实现的也是将这些类别显示为纯文本(没有<a href="..."
)。有人可以帮忙吗?
$taxonomy = 'produkte_kategorie'; // change this to your taxonomy
$terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
if( $terms ) {
echo '<ul class="p-kategorie">';
$terms = trim( implode( ',', (array) $terms ), ' ,' );
wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms );
echo '</ul>';
}
答案 0 :(得分:0)
您可以通过以下功能
来实现$terms = trim( implode( ',', (array) $terms ), ' ,' );
$categories = get_categories(array('include'=>$terms,'taxonomy'=>'produkte_kategorie'));
foreach ($categories as $category) {
echo $category->cat_name;
}
答案 1 :(得分:0)
实际上这对我没有用,但是我用wordpress codex的代码做了一个解决方案:https://developer.wordpress.org/reference/functions/wp_list_categories/
$ taxonomy ='category';
//获取分配给帖子的术语ID。 $ post_terms = wp_get_object_terms($ post-&gt; ID,$ taxonomy,array('fields'=&gt;'ids'));
//链接之间的分隔符。 $ separator =',';
if(!empty($ post_terms)&amp;&amp;!is_wp_error($ post_terms)){
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( array(
'title_li' => '',
'style' => 'none',
'echo' => false,
'taxonomy' => $taxonomy,
'include' => $term_ids
) );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// Display post categories.
echo $terms;
非常感谢您的反馈