首先,使用为给定类型的帖子注册分类法get_object_taxonomies()
. 然后检查哪些是分层的,例如使用is_taxonomy_hierarchical()
conditional tag. 最后一步是显示列表。
// current post ID
$post_id = get_the_ID();
// taxonomies registered for this type of posts
$taxonomies = get_object_taxonomies( get_post_type() );
$taxs_tree = []; // hierarchical taxonomies
$taxs_flat = []; // non-hierarchical taxonomies
foreach ( $taxonomies as $tax )
{
if ( is_taxonomy_hierarchical( $tax ) )
$taxs_tree[] = $tax;
else
$taxs_flat[] = $tax;
}
从每个分类中获取术语并显示它们。
// get terms as links
$terms_flat = $terms_tree = [];
foreach ( $taxs_tree as $tax ) {
$terms_tree[] = get_the_term_list( $post_id, $tax );
}
foreach ( $taxs_flat as $tax ) {
$terms_flat[] = get_the_term_list( $post_id, $tax );
}
echo \'<span id="one">\';
foreach ( $terms_tree as $links ) {
echo $links;
}
echo \'</span>\';
或:
//
// terms assigned to post, ordered by name, from all hierarchical taxonomies
$args = [
\'taxonomy\' => $taxs_tree, // here you can pass array of taxonomies
\'object_ids\' => get_the_ID(), // get only terms assigned to this post (current post)
//\'orderby. => \'name\', // default
];
$terms_tree = get_terms( $args ); // array of WP_term objects
//
// terms assigned to post, ordered by name, from all non-hierarchical taxonomies
$args[\'taxonomy\'] = $taxs_flat;
$terms_flat = get_terms( $args ); // array of WP_term objects
//
// display
echo \'<span id="one">\';
foreach ( $terms_tree as $term ) {
$link = get_term_link( $term );
if ( is_wp_error( $link ) ) {
continue;
}
echo \'<a href="\' . esc_url( $link ) . \'" rel="tag">\' . $term->name . \'</a> \';
}
echo \'</span>\';
// the same with $terms_flat
第二种解决方案(使用
get_terms
) 对术语排序
from all 按名称划分的层次分类法,而第一个
get_the_term_list
) 对术语排序
from each 层次分类法
separately.
参考资料:
get\\u terms()-function, parameters
get_term_link()get_the_term_list()