下面的代码将首先打印出所有顶级类别,然后打印出所有非顶级类别,如果这是您想要的。
<?php
$categories = get_the_category( get_the_ID() );
if( $categories ){
$output = "";
//display all the top-level categories first
foreach ($categories as $category) {
if( !$category->parent ){
$output .= \'<a href="\' . esc_url( get_category_link( $category->term_id ) ) . \'" title="\' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . \'" >\' . $category->name.\'</a>,\';
}
}
//now, display all the child categories
foreach ($categories as $category) {
if( $category->parent ){
$output .= \'<a href="\' . esc_url( get_category_link( $category->term_id ) ) . \'" title="\' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . \'" >\' . $category->name.\'</a>,\';
}
}
echo trim( $output, "," );
}
?>
因此,您可以使用上面的代码来代替\\u category()