The wpse244577_list_terms()
function below uses wp_list_categories()
to do the heavy lifting, then modifies the results so that the terms are in reverse order of the hierarchy.
Place this code in a plugin or your theme’s functions.php file:
/**
* 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;
}
}
To use the function, call it within the loop of your theme, passing the desired taxonomy name as the first parameter:
wpse244577_list_terms( 'property_location' );