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 ?

4 Answers 4


get_ancestors() should do what you need:

So, you should do something like this:

$ancestors = get_ancestors($term_id, 'projects_category)
  • But does get_ancestors() work with custom post type? It doesn't seem to be working for me...
    – Andy
    Jul 13, 2011 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, 2011 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, 2011 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, 2011 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, 2011 at 11:09

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

  • 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, 2011 at 8:34
  • Ahh, I see you mean, sorry I don't think I have the answer to that problem either.
    – Nsokyi
    Jul 13, 2011 at 9:31

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

$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>';

This is very good code for get taxonomy title in detail page or single page in custom post type in WordPress,

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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