Hi Ben,
Thank you for contacting us and I'll be happy to assist.
There is no built-in shortcode available to show taxonomy terms, separated based on the hierarchy level.
To achieve this, you can register a custom shortcode, that gets all the terms attached to a post and then only show them, based on the provided "level" attribute.
The following code can be added to the active theme's "functions.php" file:
add_shortcode('get_terms_by_level', 'get_terms_by_level_func');
function get_terms_by_level_func($atts) {
$taxonomy = $atts['taxonomy'];
$postid = $atts['id'];
$level = $atts['level'];
if( !(isset($level)) || (empty($level)) ) {
$level = 1;
}
$terms = get_the_terms( $postid, $taxonomy);
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ($terms as $term) {
if($term->parent == 0) {
$results['1'] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);
} else {
$tmpresults[] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);
}
}
foreach ($tmpresults as $result) {
if($result['parent'] == $results['1']['id']) {
$results['2'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
}
else {
$results['3'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
}
}
return '<a href="' . esc_url( get_term_link( $results[$level]['id'], $taxonomy) ) . '">' . $results[$level]['name'] . '</a>';
}
}
After that, in your view, you can use this new shortcode to show terms at different levels like this:
// get country level term with level 1
[get_terms_by_level id="[wpv-post-id]" taxonomy="category" level="1"]
// get city level term with level 2
[get_terms_by_level id="[wpv-post-id]" taxonomy="category" level="2"]
// get area level term with level 3
[get_terms_by_level id="[wpv-post-id]" taxonomy="category" level="3"]
Note: Please replace "category" with the actual slug of your custom taxonomy.
I hope this helps and please let me know if you have any questions around this approach.
regards,
Waqar