terms - wp_get_post_terms not working as expected

admin2025-01-07  3

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

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

Any help much appreciated.

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.

Share Improve this question edited Jan 2 at 11:37 aequalsb 1379 bronze badges asked Jun 18, 2013 at 14:53 SolaceBeforeDawnSolaceBeforeDawn 4855 silver badges21 bronze badges 5
  • 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 Commented Jun 18, 2013 at 14:57
  • Thanks - I got this from the codex. Are you saying this $count = count($terms); should be $count = $terms->count; instead? – SolaceBeforeDawn Commented Jun 18, 2013 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 Commented Jun 18, 2013 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 Commented Jun 18, 2013 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. – SolaceBeforeDawn Commented Jun 18, 2013 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 2

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>";
        }
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736253779a151.html

最新回复(0)