首页 > 解决方案 > 显示 Wordpress 子项和父项

问题描述

我有一个“位置”分类法,我希望在存档页面上显示帖子位置术语。每个帖子都有一个子术语,因此在父级 US 下标记为“纽约”。

在我希望展示的前端 - 美国纽约

所以打勾的子项,然后是父项。

目前我只有

<?php echo get_the_term_list( $post->ID, 'location'); ?> 

它只显示了勾选的术语,但我希望它也能引入父术语。

标签: phpwordpresstaxonomy-terms

解决方案


为了得到当前职位条款的父条款

$terms = wp_get_post_terms($post->ID, 'location');

foreach ($terms as $term) {
 echo get_term_link( $term ); // get the child term url
 echo get_term_link( $term->parent ); // get parent term url
 $parent = get_term($term->parent, 'location');
 echo $parent->name;
}

推荐阅读