• I’ve been able to put in my shortcode the list of the tags, but I have to better define this stuff. Now I have

    $post_tags = get_the_tags();
        $separator = ', ';
        $output = '';
    
        if ( ! empty( $post_tags ) ) {
            foreach ( $post_tags as $tag ) {
                $output .= $tag->name . $separator;
            }
        }
    

    And output with . trim( $output, $separator )

    But not all the tag are “facilities”, i.e: “pets allowed” (the tag is “animali permessi”), so, how can I print the list of all the tags except a tag “x”? I would print “x” separately beginning with a capital letter and ending with a dot. so with another specific variable. I will have also the problem to get the tags translated. I suppose for each tag I have to create a field and then a filter, like for my taxonomy fields+ filter: https://pastebin.com/4c6Qi54B

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 21 total)
  • Moderator bcworkz

    (@bcworkz)

    Tags are a type of taxonomy, so whatever you do for other taxonomies you should probably do for tags.

    You can capitalize the first letter of a word with CSS with text-transform: capitalize;. You may need to add span tags to limit application of the style. For more info see this SO question and answers.

    To exclude a certain tag name from the list, you could conditionally apply the continue statement to skip over the current name. For example:

            foreach ( $post_tags as $tag ) {
                if ('x' == $tag->name ) continue;
                $output .= $tag->name . $separator;
            }

    There are other methods to accomplish the same thing.

    Thread Starter sacconi

    (@sacconi)

    What I could use to modify the following code for tags fields? I have red that edit_tag_form_fields is now deprecated. If I have well understood I should use tags_edit_form_fields , so I just need to replace the word “tipologia” by “tags”

    add_action( 'tipologia_edit_form_fields', "tipologia_add_term_field");
    
    function tipologia_add_term_field( $term ){
       $tipologia = get_term_meta( $term->term_id, 'function_tipologia', true );
        echo '<tr class="form-field">
          <th scope="row"><label for="function_tipologia">Nome tedesco</label></th>
          <td><input type="text" name="function_tipologia" value="'.$tipologia.'"  />
          <p class="description">Nome del termine in tedesco</p></td>
        </tr>';
    }
    
    add_action( 'saved_tipologia', 'update_tipologia');
    function update_tipologia ( $term_id ) {
      update_term_meta( $term_id, 'function_tipologia', sanitize_text_field ( $_POST['function_tipologia'] ) );
    
    }
    
    Moderator bcworkz

    (@bcworkz)

    The tags taxonomy is actually named “post_tag” in PHP code, so the action hook should be 'post_tag_edit_form_fields'. Similarly, for tipologia, 'tipologia_edit_form_fields'and 'saved_tipologia' would be correct.

    What you have thus looks OK. The real question is “Does it work?” If your entry still appears in the field after saving, then presumably it’s working.

    Thread Starter sacconi

    (@sacconi)

    Ok, the field is created and the translation saved, now I have to filter according to locale. I already have a model to follow I write it here down but the main difference will be that the translated tags will appear in a post page, not in the archive page. So what “hooks” should be modified?

    add_filter('get_the_archive_title', function( $title_archive) {
       $locale = get_locale();
       if ('de_DE' == $locale ) {
    
       $title_archive_de = get_term_meta( get_queried_object()->term_id, 'function_tipologia', true );
    
         if ( ! empty( $title_archive_de )) {
            return $title_archive_de;
         } else {
            return $title_archive;
         }
       } else {
         return $title_archive;
       }
    }, 10, 2 );
    
    Moderator bcworkz

    (@bcworkz)

    Similar to the archive page, the correct hook depends upon what functions the post page uses to output what you want to translate.

    What I’ve done before you could do for yourself. (maybe 😉 ) If you can determine the function, find its source code in the code reference. Near the end of the function’s declaration there is usually a filter you can use to alter output. If there is none, there’s likely one in a sub-function. For example, the_content() has no usable filter, but it calls get_the_content() which does have a filter.

    Thread Starter sacconi

    (@sacconi)

    I tryed this with no effect

    // FILTRA I TAG IN TEDESCO
    
    add_filter('get_the_content', function( $the_content) {
       $locale = get_locale();
       if ('de_DE' == $locale ) {
    
       $the_content_de = get_term_meta( get_queried_object()->term_id, 'function_post_tag', true );
    
         if ( ! empty( $the_content_de )) {
            return $the_content_de;
         } else {
            return $the_content;
         }
       } else {
         return $the_content;
       }
    }, 10, 2 );
    

    but it seems to me the tags are not in the content, I inspected content.php and it’s not clear, they are in a sort of “no men land”. I put here my file: https://pastebin.com/vq4VrHcf

    Moderator bcworkz

    (@bcworkz)

    Sorry for the confusion. “get_the_content” was just an example for how to find the right filter. It is most definitely not the right filter!

    I’m currently on a University network and they appear to be blocking pastbin.com. I should return to my usual location on Friday. Thus, for now, I’m unable to tell you the right filter based on what you posted.

    I’m pretty sure English is not your first language and I’m glad you can communicate so well. You would cringe at my Italian skills. You’re even aware of some idioms. Kudos! So you may as well learn the correct form. It’s “No man’s land”. It’s a possessive form, not the plural form “men”. It’s not my intent to be critical, my only wish is to better inform.

    Thread Starter sacconi

    (@sacconi)

    “No man’s land” is a simple genitive. I studied it but I should concentrate myself more when I write in english, which is not my first language (not even the second). I got a lot of minus from my teacher of english, I couldn’t bare reading “In a free state” by Neipaul…it was boring, anyway whatever the subject I have difficulty keeping my concentration on something for too long, for php is the same 🙁

    Thread Starter sacconi

    (@sacconi)

    May content.php is her: https://pastebin.com/vq4VrHcf , I cant see the right “hook” to filter accoding to the locale .de

    this is my code to be modified

    // FILTRA I TAG IN TEDESCO
    
    add_filter('get_the_content', function( $the_content) {
       $locale = get_locale();
       if ('de_DE' == $locale ) {
    
       $the_content_de = get_term_meta( get_queried_object()->term_id, 'function_post_tag', true );
    
         if ( ! empty( $the_content_de )) {
            return $the_content_de;
         } else {
            return $the_content;
         }
       } else {
         return $the_content;
       }
    }, 10, 2 );
    Moderator bcworkz

    (@bcworkz)

    I don’t see where tags are output. The only reference to tags is if the property has the animali permessi tag, it outputs an image image. Maybe the others are output via shortcode? If so, you could modify the shortcode handler to do any needed translations just before they are added to the output. Then there’s no need to be sure you are only translating the correct tags since any tag names being output would be the correct tags to translate.

    Thread Starter sacconi

    (@sacconi)

    I’m trying to modify the shortcode

    I have the following about the tag field in german (I dont write here the update)

    add_action( 'post_tag_edit_form_fields', "post_tag_add_term_field");
    
    function post_tag_add_term_field( $term ){
       $post_tag = get_term_meta( $term->term_id, 'function_post_tag_de', true );
        echo '<tr class="form-field">
          <th scope="row"><label for="function_post_tag_de">Nome tedesco</label></th>
          <td><input type="text" name="function_post_tag_de" value="'.$post_tag.'"  />
          <p class="description">Nome del tag in tedesco</p></td>
        </tr>';
    }
    

    This is my code in the shortcod, about tags

    $post_tags = get_the_tags();
        $separator = ', ';
        $output = '';
    
        if ( ! empty( $post_tags ) ) {
            foreach ( $post_tags as $tag ) {
    if ('animali permessi su richiesta' == $tag->name ) continue;
                $output .= $tag->name . $separator;
            }
        }
    

    I think I should add to the above code:

    $locale = get_locale();
    
    $post_tag = get_term_meta( $term->term_id, 'function_post_tag_de', true );

    but I cant see which logic I should follow. I’d like a function for all the languages having in mind that function_post_tag_de could be with _en for english and so on

    Moderator bcworkz

    (@bcworkz)

    You need to decide on the desired strategy before doing anything further. Will you get tag name translations from term meta? Or will you get tag name translations via gettext and an array like $names_trans?

    Without that decision, the only logic we can apply so far is:
    Is the locale “it_IT”? If so, proceed normally. If not, get translated names for current locale.

    Thread Starter sacconi

    (@sacconi)

    I tryed this but it doesnt work

    $post_tag = get_term_meta( $term->term_id, 'function_post_tag_'.$lang, true );
    
    $post_tags = get_the_tags();
        $separator = ', ';
        $output = '';
    
        if ( ! empty( $post_tags )  && ($locale="it_IT")) {
            foreach ( $post_tags as $tag ) {
    if ('animali permessi su richiesta' == $tag->name ) continue;
                $output .= $tag->name . $separator;
            }
        } else
    
    { $output .= $post_tag . $separator;
    
    }
    
    Moderator bcworkz

    (@bcworkz)

    $locale="it_IT" Here you are assigning (single = )a value to $locale, not comparing (double ==). This will not yield correct behavior.
    $locale =="it_IT" is what you want and will result in this logic: if $post_tags is not empty AND if $locale is “it_IT”, THEN do the loop and concatenate the Italian tag names.

    So far so good. But $output .= $post_tag . $separator; for other languages is not right. $post_tag is undefined, you will cause an error. There is not a single tag name to translate. You must loop through all the tags in $post_tags just like you’ve done for the Italian tag names, except now instead of concatenating Italian tag names, you would concatenate the translated tag names.

    Where would the translations come from? From $names_trans and gettext? Or from term meta?

    Thread Starter sacconi

    (@sacconi)

    translations come from term meta

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘printing tags name and their translation’ is closed to new replies.