Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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) {
        $i++;
        $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

Try this:

<?php
  $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 :( –  Ziya Aliyev 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?) –  Ziya Aliyev Jun 28 '12 at 11:44
    
Maybe i must use $post->ID somewhere? –  Ziya Aliyev Jun 28 '12 at 11:53
1  
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

Your Answer

 
discard

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.