Your function taxonomy_hierarchy()
is not actually linked to the shortcode. Your function taxonomy_hierarchy()
is also not a proper shortcode function.
You can find how to create proper shortcodes here.
A shortcode function also has to return, don’t use echo.
This is how your function taxonomy_hierarchy
should kinda look. I changed the code for your needs:
function taxonomy_hierarchy() {
global $post;
$return = '';
$terms = wp_get_post_terms( $post->ID, 'property_city' );
foreach ( $terms as $term ) {
// this gets the parent of the current post taxonomy
if ( $term->parent != 0 ) {
$return .= $term->name ', ' . get_term( $term->parent, 'property_city' )->name;
} else {
$return .= $term->name;
}
}
return $return;
}
Then add this to create the shortcode from the function taxonomy_hierarchy()
.
add_shortcode( 'child-parent', 'taxonomy_hierarchy' );
When using the shortcode frontend, you can then use [child-parent]
. Backend you can use echo do_shortcode( '[child-parent]' );