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 have a custom post type called "Fruit" and under this I have a hierarchical taxonomy called, "Fruit Categories"

When I create a new post under Fruit, I am able to assign it to a "term" - like you would with a normal post category, but under the Fruit Categories taxonomy.

I've added three posts to Fruit, with each assigned to a single term respectively; "Apples", "Pears" and "Bananas".

All this works fine, but now I want to create an archive page that simply lists the terms in a UL, for that custom post type, like so;

mysite.com/fruit

  • Apples
  • Pears
  • Bananas

This problem I am facing is that every query I have used only seems to return the first term, Apples. The other two are not showing and I've tried many code snippets, all with the same avail.

Some things I've tried;

    $term_list = wp_get_post_terms($post->ID, 'fruit_categories', array("fields" => "all"));  print_r($term_list);

And

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

if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
        echo '<li><a href="'.get_term_link($term->slug, 'fruit_categories').'">'. $term->name . "</a></li>";
    }
    echo "</ul>";
}

I also tried the tips suggested here, however, this still results in the same issue with just the first term being listed.

http://stackoverflow.com/questions/15502811/display-current-post-custom-taxonomy-in-wordpress

It's a fresh install with no plugins.

Do I need to add a foreach loop to pull in the additional terms? Or is there something I am missing here?

I've also been looking at wp_get_object_terms but I don't really understand how to use it.

When I do a screen dump, I get the following on mysite/fruit

Apples

Array ( [0] => stdClass Object ( [term_id] => 43 [name] => Apples[slug] => apples [term_group] => 0 [term_taxonomy_id] => 43 [taxonomy] => fruit_categories [description] => [parent] => 0 [count] => 2 ) )

For what it's worth, here is my Custom Post Type and taxonomy registry code http://pastebin.com/K8kwuzqt

Any help much appreciated.

share|improve this question
    
For count you should reference $terms->count instead, as the return of the wp_get_post_terms contains more than a simple array. codex.wordpress.org/Function_Reference/wp_get_post_terms –  GhostToast Jun 18 '13 at 14:57
    
Thanks - I got this from the codex. Are you saying this $count = count($terms); should be $count = $terms->count; instead? –  Sol Jun 18 '13 at 14:59
    
Actually I tried your main code block and it worked perfectly. So, you might be calling something else incorrectly. Perhaps it only does have the one term? Nevermind what I said about count –  GhostToast Jun 18 '13 at 15:16
2  
After you define $terms, try doing var_dump($terms) to see what it contains. This is a useful way to debug an object. –  GhostToast Jun 18 '13 at 15:20
    
Hmmm well then there is something else amiss for me, as this definitely only pulls in the single term, even though each term is being used. Thanks anyway. –  Sol Jun 18 '13 at 15:21

1 Answer 1

up vote 2 down vote accepted

I misunderstood what you were trying to do before. I thought you wanted to list the terms associated with one particular post - the one you are on. Whoops!

Try this instead:

    $terms = get_terms('fruit_category');
    if(!empty($terms)){ 
        echo "<ul>";
        foreach ( $terms as $term ) {
            echo '<li><a href="'.get_term_link($term->slug, 'fruit_categories').'">'. $term->name . "</a></li>";
        }
        echo "</ul>";
    }

This will get you a list of all the links to the term pages, provided each term has at least one post in it.

Update:

To get taxonomy terms a bit more dynamically, can do this:

// taxonomy term archives
$post_type = get_post_type();
$taxonomies = get_object_taxonomies($post_type);
if(!empty($taxonomies)){
    foreach($taxonomies as $taxonomy){
        $terms = get_terms($taxonomy);
        if(!empty($terms)){ 
            echo "<ul>";
            foreach ( $terms as $term ) {
                echo '<li><a href="'.get_term_link($term->slug, $taxonomy).'">'. $term->name . "</a></li>";
            }
            echo "</ul>";
        }
    }
}
share|improve this answer
    
Ah - that's it ! Thank you GhostTrain - can't believe that is so simple and I missed it. It is now correctly outputting the 3 fruit categories correctly. Big ups to you!! :-) –  Sol Jun 18 '13 at 15:44
    
Lol GhostTrain. No problem. Sorry I led you around the wrong way before. –  GhostToast Jun 18 '13 at 15:44
    
haha sorry - I was so excited I can't read ! LOL –  Sol Jun 18 '13 at 15:45
    
GhostToast - can you help a little more - I promise to get your name right this time (though my own folly did make me laugh) How would I modify $terms = get_terms('fruit_category'); to check dynamically for the CPT (according to the current page) - i.e. without having to specify "fruit_category" ? –  Sol Jun 18 '13 at 15:50
    
Will add to answer as it won't look right here... –  GhostToast Jun 18 '13 at 15:52

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.