WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to echo my hierarchical taxonomies parents which I used in my posts.
I've see answers about that, but no one didn't help me.

To get all taxonomies, currently I use

<?php the_terms( $post->ID, 'mytaxname', '', ' / ', ' ' ); ?>`

How to get only parents of used tax's?

share|improve this question
up vote 0 down vote accepted

Try this:

  $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
  $parent = get_term($term->parent, get_query_var('taxonomy') );

  echo $parent->name;

You have to get the current term slug and then use get_term by the slug and then echo the name.

share|improve this answer
Thank you for answer. But this code doesn't echo anything. I changed 'taxonomy' to my taxonomy name, nothing happens :( – Zia Jun 28 '12 at 10:40
I dont think you need to change taxonomy to something else. – Pontus Abrahamsson Jun 28 '12 at 11:11
Look, my hierarchical taxonomy name (slug) is called "Role". How WordPress must recognize, which taxonomy i want to echo?) – Zia Jun 28 '12 at 11:44
Maybe i must use $post->ID somewhere? – Zia Jun 28 '12 at 11:53
Also -100000 for calling your taxonomy "Role" that's a reserved name, change it to something else as you'll have lots of horrible, painful problems – Tom J Nowell Jan 28 '13 at 16:21

I'm not the BEST with PHP, but... This returns a list of taxonomy terms attached to a post via the taxonomy name. It will only display the parent terms of the taxonomy names.

$args = array( 'taxonomy' => 'YOUR_TERM_NAME', 'parent' => 0 );
$terms = get_terms('YOUR_TERM_NAME', $args);
$count = count($terms); $i=0;
if ($count > 0) {
    foreach ($terms as $term) {
        $term_list .= '<a href="?YOUR_TERM_NAME=' . $term->slug . '" title="' . sprintf(__('View all post filed under %s'), $term->name) . '">' . $term->name . '</a>';
        if ($count != $i) $term_list .= ' &middot; '; else $term_list .= '';
    echo $term_list;

It will also link to the archive page for each term. You can repeat this within your post's loop as many times as you like for each taxonomy you want to display. That's the part that I feel isn't very clean, I'm sure there's a way to display multiple taxonomies in one function, but I can't do that. The above achieves what you want, though.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.