0
votes

I have a 'location' taxonomy, and on the archive pages I wish to display the posts location terms. Each post has one child term, so it is ticked 'New York' under the parent US.

On the front end I wish to display - New York, US

So the child term which is ticked, and then the parent.

At the moment I just have

<?php echo get_the_term_list( $post->ID, 'location'); ?> 

Which just shows the ticked term, however I wish it to pull in the parent term too.

1

1 Answers

1
votes

In order to get the parent terms of current post terms

$terms = wp_get_post_terms($post->ID, 'location');

foreach ($terms as $term) {
 echo get_term_link( $term ); // get the child term url
 echo get_term_link( $term->parent ); // get parent term url
 $parent = get_term($term->parent, 'location');
 echo $parent->name;
}