Title: Extending a function
Last modified: February 5, 2024

---

# Extending a function

 *  [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/extending-a-function/)
 * How can I insert this:
 *     ```wp-block-code
       <strong> <?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?> </strong>
       ```
   
 * to this:
 *     ```wp-block-code
       add_shortcode('incipit', 'translate_incipit');
       function translate_incipit() {
         global $post;
         $german = get_post_meta( $post->ID, 'incipit_de', true );
         if ( 'de_DE' == get_locale() && ! empty( $german )){
            return $german;
         } else {
            return get_post_meta( $post->ID, 'incipit_it', true );
         }
       }
       ```
   
 * what I want to do is having the category name printed before the value of “get_post_meta”,
   having in mind that I should print the name of the category eventually translated
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fextending-a-function%2Fpage%2F3%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 13 replies - 31 through 43 (of 43 total)

[←](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)
[1](https://wordpress.org/support/topic/extending-a-function/?output_format=md) 
[2](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)
3

 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17437084)
 * “get_the_category” instead of “wp_get_post_categories” (and the rest is unchanged)
   breaks the layout
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17440212)
 * Works as expected for me:
 *     ```
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
          global $post;
          $lang = substr( get_locale(), 0, 2);
          $category = get_the_category( $post->ID )[0]->name;
          $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
          return  $category.' '. $content; 
       }
       ```
   
 * I’m not clear why you’d have a term’s name followed by a post’s description. 
   Isn’t the term name Italian while the post description is German? I’d think you
   would want a term name in appropriate language followed by that term’s description
   in the appropriate language. You can do what you like, but I don’t know what 
   that is. In any case the code works as expected.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17440381)
 * you are right, it’s working. Theoretically I would need the category translated
   in the right language. Since the cathegoy terms are all resorts, little places
   most of them in Italy, normally they dont have a different name in a foreigner
   language. I need to do the same also for the taxonomy “tipologia” (I need the
   translation this time). Cathegory and taxonomy should have a class in order to
   work on them via CSS. If you see this page [https://test.sacconicase.com/lignano-riviera-appartamento-per-6-persone-in-residence-con-piscina/](https://test.sacconicase.com/lignano-riviera-appartamento-per-6-persone-in-residence-con-piscina/)
   you can understand the sense of this work. If you see the description above the
   images gallery, “**Lignano riviera appartamento**” is managed by a custom field(
   incipit_it and incipit_de), too many fields to fill! So, I’m trying to have the
   same results using just the fields for the description, and I will delete the“
   incipit” fields.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17440613)
 * Is this the right way to print also taxonomy terms in the right language?
 *     ```wp-block-code
       add_shortcode('description', 'translate_desc');
       function translate_desc() {
          global $post;
          $lang = substr( get_locale(), 0, 2);
          $category = get_the_category( $post->ID )[0]->name;
       $terms = get_terms( array(
           'taxonomy'   => 'tipologia',
           'hide_empty' => false,
       ) );
          $content = get_post_meta( $post->ID, 'description_'.$lang, true );     
          return  $category.$terms .' '. $content; 
       }
       ```
   
 * At the moment $terms prints “Array”
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17443684)
 * get_terms() by default returns an array of WP_Term objects. If you only need 
   the names you could add `'fields' => 'names',` args array. But if you will also
   need a translation from term meta, you’ll need the ID. You can instead go ahead
   and get the array of objects and extract out the names later.
 * I’m confused though. Why would you want a list of all tipologia terms in a [description]
   shortcode? Wouldn’t you only want terms assigned to the current post? If so you
   should use `get_the_terms()`. It works the same as get_the_category(), except
   you have to also pass the taxonomy slug as a second parameter. Similarly, if 
   there is only one term assigned to any given post, it’ll always be the zeroth
   term in the array, so you could do:
    `$terms = get_the_terms( $post->ID, 'tipologia')[
   0]->name;`
 * I think you’d want a space between $category and $terms? If you like, instead
   placing `.' '.` between each variable in the return line, use a double quoted
   string. When double quoted, PHP will automatically concatenate variable values
   without you needing the `.` operator. Like so:
    `return "<p>$category $terms</
   p><p>$content</p>";` I added p tags as an example of a mix of string characters
   and variables. You don’t need them if you don’t want them.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17444850)
 * `$terms = get_the_terms( $post->ID, 'tipologia')[0]->name;` is what I need, but
   it should print also the german version when I am on the german version of the
   web site and so on for all the languages. I need a filter. But inside this function
   or should I have another specific function?
 * Should I exploit somewhere `'tipologia_'.$lang` ?
 * For the css I did this way:
 *     ```wp-block-code
       return '<span class="incipit_description">'. $category . ' '. $terms . '</span>' . 
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17446587)
 * `'tipologia_'.$lang` will be good when you add other languages. You also need
   a conditional to do nothing if the locale is “it_IT”.
 * My double quote suggestion gets a little more complicated when the string includes
   double quotes as well. They need to be escaped with a `\`. Your return line example
   in double quote style would be:
    `return "<span class=\"incipit_description\"
   >$category $terms</span>";` I know the entire line was clipped in your last reply,
   so this example is clipped as well. I think you can see the concept though. Either
   way works, do as you wish.
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17450102)
 * I have had an idea! Since I found a code for a custom box with a selector and
   I’ve been able to put gettext in the array, why should I do something similar
   for the taxonomy “tipologia”? The terms of this taxonomy are few and are all 
   the same, I dont have to add other terms all the time, so instead of filtering
   the terms according to the locale I think it’s better to have a list of pre-written
   and gettexted terms, like in this piece of code:
 *     ```wp-block-code
       $piani = array(
               __(' 1st floor, ','sacconicase') => ' 1°piano ',
                 __(' 2nd floor, ','sacconicase') => ' 2°piano ',
   
         __(' 3rd floor, ','sacconicase') => ' 2°piano ',
   
           );
       ```
   
 * Now in my shortcode I have `$terms = get_the_terms( $post->ID, 'tipologia')[0]-
   >name;`
 * and I return `$terms`
 * I suppose I have to go back to
 *     ```wp-block-code
       $terms = get_terms( array(
           'taxonomy'   => 'tipologia',
           'hide_empty' => false,
       ) );
       ```
   
 * but where and how can I add my gettexted terms??
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17450588)
 * Gettext is intended for static strings that never change, such as “Submit” for
   button labels. We normally think of taxonomy terms as non-static since they are
   user defined and subject to change. However, if you know ahead of time what all
   the term names will be and they will never be altered, then it’s feasible to 
   use gettext to translate the term names since they are in essence “static”
 * So if term names are predefined in the gettext system, you could do something
   like:
 *     ```
       $terms = get_the_terms( $post->ID, 'tipologia')[0]->name;
       $term_trans = __( $terms, 'sacconicase');
       ```
   
 * We’re not supposed to pass variables to __() but AFAIK it will work anyway. There’s
   a way to not pass variables and still translate term names, but it’s more cumbersome.
   For example, we could construct a static associative array of translations and
   access the desired term translation using the term’s ID. It’s a silly procedure
   just to get around the no variables rule. It’s much simpler to just break the
   rule 😉
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17451059)
 * I added `$term_trans = __( $terms, 'sacconicase');` but my plug in loco translate
   cant extract the terms for the translation. Ps: I havent been notified by email
   as usual about your last replies…I thought you were on holidays 🙂
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17451690)
 * Sorry, wp.org is experiencing outgoing email issues and no one is getting notifications.
   Until that’s resolved you’ll need to check for replies manually. I recommend 
   using [https://wordpress.org/support/users/sacconi/replied-to/](https://wordpress.org/support/users/sacconi/replied-to/)
   to check for recent replies until notifications start working again. As you’ve
   likely noticed, I typically check topics and reply once a day starting around
   16:00 UTC or later. If you’re waiting on me, no need to check very often.
 * For gettext to work, the term names have to appear in the .po language file, 
   then be compiled into a .mo file. I don’t know how to get Loco Translate to do
   that. It could be ignoring variables when it parses your code to compile the .
   po file. You might need to place non-functional translation code in order to 
   get the term names to be translated. For example:
 *     ```
       __('1st Floor', 'sacconicase');
       __('2nd Floor', 'sacconicase');
       // etc....
       ```
   
 * Keep using $terms in the functional code. This could reside on its own in functions.
   php. But if we need to do this anyway, it might make sense to turn this into 
   a usable static associative array that I was thinking about earlier. Then we 
   would actually not be breaking any rules. An array along the lines of:
 *     ```
       $names_trans = array(
          'term_id_key_1' => __('1st Floor', 'sacconicase'),
          'term_id_key_2' => __('2nd Floor', 'sacconicase'),
          // etc....
       );
       ```
   
 * Replace ‘term_id_key_1’, etc. with the actual term ID values. Then we can access
   the translated term names through the term ID key. The only problem with this
   is it becomes increasingly difficult to add or alter term names. The back end
   UI will be nearly useless, any changes will involve code editing.
    -  This reply was modified 2 years, 3 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
 *  Thread Starter [sacconi](https://wordpress.org/support/users/sacconi/)
 * (@sacconi)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17452382)
 * I think something is missing, probably a logic connection between the 2 declarations:
 *     ```wp-block-code
       $terms = get_the_terms( $post->ID, 'tipologia')[0]->name;
       $names_trans = array(
          'term_id_key_350' => __('Apartment', 'sacconicase'),
          'term_id_key_354' => __('Apartment in villa', 'sacconicase'),
          'term_id_key_355' => __('Penthouse', 'sacconicase'),
   
       'term_id_key_357' => __('Bungalow', 'sacconicase'),
   
       'term_id_key_356' => __('Studio', 'sacconicase'),
   
       'term_id_key_367' => __('Villa', 'sacconicase'),
   
       'term_id_key_351' => __('Terraced villa', 'sacconicase'),
   
       );
       ```
   
 * gettext is working, loco translate extracts the strings, but $terms is always
   returned in italian, not with english name
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17453737)
 * Replace the key string in each `'term_id_key_350' => __('Apartment', 'sacconicase'),`
   line with just the ID integer itself, as in:
    `350 => __('Apartment', 'sacconicase'),`
 * Then after declaring the entire array you can do:
    `$terms = $names_trans[ get_the_terms(
   $post->ID, 'tipologia')[0]->term_id ];`
 * BTW, notifications should be working again. Previously missed notifications will
   not be resent, but new replies should send notifications. If you still are not
   getting them let me know.

Viewing 13 replies - 31 through 43 (of 43 total)

[←](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)
[1](https://wordpress.org/support/topic/extending-a-function/?output_format=md) 
[2](https://wordpress.org/support/topic/extending-a-function/page/2/?output_format=md)
3

The topic ‘Extending a function’ is closed to new replies.

## Tags

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

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 43 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [2 years, 3 months ago](https://wordpress.org/support/topic/extending-a-function/page/3/#post-17453737)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
