Wordpress- How to Get Term Name from Term_Id?

I'm trying to retrieve term names from taxonomies for $title. I have come across a lot Codex functions like get_post_meta(), get_the_terms() etc, which seem to only get term name from post_id, which is not what I'm looking for.

How do I get term names from term_id?

4

3 Answers

You may get the term name from term_id like this:

$term_name = get_term( $term_id )->name;

Explanation: get_term() returns the term object and name is one of propeties of this object.

More details on codex:

Please try this:

<?php $term = get_term_by( $field, $value, $taxonomy); ?>

Note:

  • $field => Just write 'id' here
  • $value => Place your 'term_id' value here
  • $taxonomy => write your custom taxonomy 'slug' here

For ex: My custom taxonomy slug is 'services' & 'term_id' is 5, so here is the code for retrieving 'term_name':

<?php $term = get_term_by( 'id', 5, 'services' ); 
echo $term->name; ?>

I hope, this may be helpful to you.

4
 $term = get_term_by('term_id', '1', 'category'); 
 $name = $term->name; 

Your Answer

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

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest