1

I have been searching google but I am really confused. I am trying to display the terms of the taxonomy assigned to the post. I am using the_terms($post->ID, 'locations');. The custom taxonomy is hierarchical.

Example: 3 terms assigned to post: USA(parent) > FL(direct child of "USA") > Miami(direct child of "FL"). What I get: FL, Miami, USA which means the terms are being displayed in alphabetical order. I want them to be displayed like: Miami, FL, USA. Can this be achieved? I would also like to remove the anchors from the terms and strip_tags(the_terms($post->ID, 'locations')) doesn't seem to work.

While searching google some people use get_terms() some other get_the_terms and others the_terms which is what I use and seems to work - output the terms. What is the difference between those functions? Am I using the right one?

3

To answer your first question

What is the difference between those functions

  • get_terms() returns an array of terms objects that belongs to a specific taxonomy

  • get_the_terms() returns an array of terms belonging to a post

  • the_terms() displays an HTML formatting string of term names belonging to a post

Because you need your terms not hyperlinked and ordered according to parent, I believe wp_get_object_terms() will be a better option here. wp_get_object_terms() also returns an array of terms belonging to a post, but is more flexible. You do pay for this flexibility though as you make an extra db call per post.

With this all in mind, you can try the following: (All code is untested)

$args = [
    'orderby' => 'parent', 
    'order'   => 'DESC' 
];
$terms  = wp_get_object_terms( $post->ID, 'locations', $args );
$names  = wp_list_pluck( $terms, 'name' );
$output = implode( ', ', $names );
echo $output;
  • Feel free to leave me a comment if you are not clear about something ;-) – Pieter Goosen Feb 4 '16 at 12:34
  • Thank you very much for your answer. It works however it seems to ignore 'order' => 'DSC'. It outputs "USA, FL, Miami" while I want this reversed "Miami, FL, USA". – Arg Geo Feb 4 '16 at 13:37
  • Nevermind, I am stupid. I used "DSC" instead of "DESC". Thanks a ton for your help Pieter! – Arg Geo Feb 4 '16 at 13:42
  • Hahaha, no problem, was just about to post DESC. Glad it worked. Enjoy ;-) – Pieter Goosen Feb 4 '16 at 13:43
-1

You should use

get_the_terms(int|object $post, string $taxonomy)

https://developer.wordpress.org/reference/functions/get_the_terms/ or

get_terms(array|string $args = array(), array $deprecated = '')

https://developer.wordpress.org/reference/functions/get_terms/

because

the_terms()

echo/prints the results

and

 get_the_terms() //or
 get_terms() 

only get value and save it to variable or memory.

A possible example is

foreach($posts as $post){
    $term= get_the_terms($post->ID, 'category' );
    echo 'Category: '.$term;
}

You may need to use another loop to handle $terms if there or more than 1 object in any case.

  • I want to display the_terms inside the loop... I am not trying to list all the taxonomy terms. – Arg Geo Feb 4 '16 at 11:37
  • Ok you can get and then echo terms inside loop.. If you are in wordpress default post loop then you can do it more easily. just post the_terms(); you don't need to provide any parameter other than custom taxonomy. I will suggest you to share your code and scenario in detail here so others can help you in effective way – Optimum Creative Feb 4 '16 at 11:40
  • It just displays the word "Array". – Arg Geo Feb 4 '16 at 11:42
  • Ok. Yes it will be do this $trm=the_term(); print_r($trm) and then share your result here. – Optimum Creative Feb 4 '16 at 11:43
  • I understand nothing. Anyway, thanks for you contribution. – Arg Geo Feb 4 '16 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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