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.
https://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.
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:
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: