Title: get_the_terms with variable
Last modified: September 1, 2016

---

# get_the_terms with variable

 *  [brian914](https://wordpress.org/support/users/brian914/)
 * (@brian914)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/get_the_terms-with-variable/)
 * I have the following two functions that I would like to combine into one, to 
   where I would pass in which term(name, slug, etc) I want to get out.
 *     ```
       function get_rug_terms_name($term) {
           $terms = get_the_terms( get_the_ID(), $term);
           if( !empty($terms) ) {
             return implode(", ", array_map(function(WP_Term $term){
               return $term->name;
             }, $terms));
           }
         }
   
         function get_rug_terms_slug($term) {
           $terms = get_the_terms( get_the_ID(), $term);
           if( !empty($terms) ) {
             return implode(", ", array_map(function(WP_Term $term){
               return $term->slug;
             }, $terms));
           }
         }
       ```
   
 * I was thinking something like:
 *     ```
       function get_rug_terms($term, $which_term)
       ```
   
 * and then return $term->$which_term;
    but that doesn’t seem to work. Feel like
   i am probably close, but just not quite getting it.
 * Any help would be great!!!

The topic ‘get_the_terms with variable’ is closed to new replies.

## Tags

 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [get_the_terms](https://wordpress.org/support/topic-tag/get_the_terms/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 0 replies
 * 1 participant
 * Last reply from: [brian914](https://wordpress.org/support/users/brian914/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/get_the_terms-with-variable/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
