这个wpse244577_list_terms()
以下功能使用wp_list_categories()
若要执行重载,请修改结果,使术语与层次结构的顺序相反。
将此代码放在插件或主题函数中。php文件:
/**
* Lists term links for a taxonomy in reverse order of hierarchy
*
* Based on https://developer.wordpress.org/reference/functions/wp_list_categories/#comment-1169
* @param $taxonomy string taxonomy name
* @param $separator string separator for terms
*/
function wpse244577_list_terms( $taxonomy, $separator = \', \' ) {
// Get the term IDs assigned to post.
$post_terms = wp_get_object_terms( get_the_ID(), $taxonomy, [ \'fields\' => \'ids\' ] );
if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
$terms = wp_list_categories( [
\'title_li\' => \'\',
\'style\' => \'none\',
\'echo\' => false,
\'taxonomy\' => $taxonomy,
\'include\' => $post_terms,
\'separator\' => $separator,
] );
$terms = rtrim( trim( str_replace( $separator, $separator, $terms ) ), \', \' );
$terms = explode( $separator, $terms );
$terms = array_reverse( $terms );
$terms = implode( $separator, $terms );
echo $terms;
}
}
要使用该函数,请在主题的循环中调用它,并将所需的分类名称作为第一个参数传递:
wpse244577_list_terms( \'property_location\' );