我自己设法找到了解决办法。此代码将在分类法模板中使用。
PHP:
function show_taxonomy_navigation( $post_type = \'post\', $taxonomy = \'category\', $parent = 0 ) {
$html = get_taxonomy_hierarchy( $post_type, $taxonomy, $parent );
echo $html;
}
function get_taxonomy_hierarchy( $post_type, $taxonomy, $parent = 0, $depth = 0 ) {
$taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
$html = \'\';
// get the id of the currently queried term
$current_term_id = get_queried_object_id();
$args = array(
\'taxonomy\' => $taxonomy,
\'parent\' => $parent,
\'order_by\' => \'name\',
);
// Get the child terms of $parent
$terms = get_terms( $args );
if ( $terms ) {
if ( $depth == 0 ) {
$html .= \'<nav class="taxonomy-navigation">\';
}
$ul_classes = [];
$ul_classes[] = \'terms\';
$html .= \'<ul class="\' . esc_attr( implode( \' \', array_filter( $ul_classes ) ) ) . \'">\';
// Add \'All\' link
$all_link = ( $parent > 0 ) ? get_term_link( $parent ) : get_post_type_archive_link( $post_type );
$html .= \'<li><a href="\' . $all_link . \'">\' . esc_html__( \'All\', \'my-taxonomy-navigation\' ) . \'</a></li>\';
foreach ( $terms as $term ) {
$li_classes = [];
$li_classes[] = \'term-item\';
// Add class if term corresponds to the currently queried term
$li_classes[] = ( $term->term_id == $current_term_id ) ? \'current-term\' : null;
// Add class if term is a parent of the currently queried term
$li_classes[] = ( term_is_ancestor_of( $term->term_id, $current_term_id, $taxonomy ) ) ? \'current-term-parent\' : \'\';
$html .= \'<li class="\' . esc_attr( implode( \' \', array_filter( $li_classes ) ) ) . \'">\';
$html .= \'<a href="\' . get_term_link( $term->term_id ) . \'">\' . $term->name . \'</a>\';
// Recursively go deeper in the hierarchy
$html .= get_taxonomy_hierarchy( $post_type, $taxonomy, $term->term_id, $depth + 1 );
$html .= \'</a>\';
$html .= \'</li>\';
}
$html .= \'</ul>\';
if ( $depth == 0 ) {
$html .= \'</nav>\';
}
}
return $html;
}
CSS:
.taxonomy-navigation ul.terms {
position: relative;
list-style: none;
text-align: center;
}
.taxonomy-navigation ul.terms li {
display: inline-block;
margin: 0;
padding: 0 15px;
}
.taxonomy-navigation ul.terms li.current-term > a,
.taxonomy-navigation ul.terms li.current-term-parent > a {
font-weight: 700;
}
.taxonomy-navigation ul.terms li ul.terms {
display: none;
position: absolute;
top: auto;
left: 0;
right: 0;
margin: 0 auto;
}
.taxonomy-navigation ul.terms li.current-term > ul.terms,
.taxonomy-navigation ul.terms li.current-term-parent > ul.terms {
display: block;
}