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.

Hierarchical taxonomy of custom post type 'projects' > 'projects_category'.

Two example 'projects_category' hierarchies would be:

Big Corporates > 1st Company Name > A Post Title

Small Business > 2nd Company Name > Another Post Title

I can get '1st Company Name' with the following:

<?php $terms = get_the_terms($post->ID, 'projects_category');foreach($terms as $term){echo $term->name;} ?>

How can I display 'Big Corporates' or 'Small Business' as appropriate in single.php ?

share|improve this question

3 Answers 3

get_ancestors() should do what you need:

So, you should do something like this:

$ancestors = get_ancestors($term_id, 'projects_category)
share|improve this answer
    
But does get_ancestors() work with custom post type? It doesn't seem to be working for me... –  Andy Jul 13 '11 at 10:47
    
Your question doesn't really have anything to do with CPT's - it's about custom taxonomies. Also, check you're on WP 3.1 or above –  anu Jul 13 '11 at 10:52
    
I'm on latest version of WP 3.2.1. I stated this is for custom post types in the first paragraph of my question but maybe it is more about custom taxonomies? Either way the answer still eludes us. –  Andy Jul 13 '11 at 10:58
    
You may be using CPTs, but what you're looking for is a way to traverse a custom taxonomy hierarchy - that's what 'terms' are - items in a taxonomy that you can attach to a post object (whether that post object is a normal post, a page, or a custom post type) –  anu Jul 13 '11 at 11:03
    
I'd tried this <?php get_ancestors( $post->ID, 'projects_category' ); ?> which returned nothing. Can you elaborate on your answer at all? –  Andy Jul 13 '11 at 11:09

If you read this article on custom post types it should help: Revisiting Custom Post Types, Custom Taxonomies, and Permalinks

share|improve this answer
    
It didn't help, I saw nothing in there that showed how to get the root taxonomy name in the single template. In the comments there are people asking how to do it but no solution was posted. I think this is not easily achieved for a custom post type. –  Andy Jul 13 '11 at 8:34
    
Ahh, I see you mean, sorry I don't think I have the answer to that problem either. –  Nsokyi Jul 13 '11 at 9:31
up vote 0 down vote accepted

I've marked up anu's answer and get_ancestors explanation, however this is how I solved it:

<?php 
$terms = wp_get_object_terms($post->ID, 'projects_category', array('orderby' => 'term_id', 'order' => 'ASC') );
    if ( !empty( $terms ) ) :
    $project = array();
    foreach ( $terms as $term ) {
        $project[] = $term->name;
}
    echo '<h1>'.$project[0].'</h1>';
    echo '<h2>'.$project[1].'</h2>';
    endif;
    ?>
share|improve this answer

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.