0

I tryed this code but the german version is not working (italian is ok)

$lang = substr( get_locale(), 0, 2 );
global $post;
if ('it' != $lang ) {
$term_name = get_the_terms(get_the_ID(), 'intro_'.$lang)[0]->name;
} else
$term_name = get_the_terms (get_the_ID(), 'intro')[0]->name;

the line with 'intro_'.$lang breaks the posts layout, any solution?

2 Answers 2

0

Here you are dealing with 2 taxonomy for getting the term name which is wrong.

You just need to do this

`$term_name = get_the_terms (get_the_ID(), 'intro')[0]->name;`

Once you get the $term_name then you can apply your locale to display it.

<?php echo __( $term_name, 'YOUR_DOMAIN' );
3
  • I'm inside a shorcode, I cant echoing. Taxonomy is just one, but I have custom fields for each taxonomy term, for german all the terms ends by "_de" and so on Commented Apr 3 at 20:02
  • Instead of echo your can use return statement.
    – Aftab
    Commented Apr 3 at 20:06
  • Your taxonomy name is "intro" so you need to use get_the_terms (get_the_ID(), 'intro')
    – Aftab
    Commented Apr 3 at 20:07
0

you are trying to access the term name in a different language. Instead of directly accessing the term name using the language code concatenated with the taxonomy name, you should consider using the wp functions provided for translation.

global $post;

// Get the current language
$lang = substr(get_locale(), 0, 2);



// Get the terms for the appropriate taxonomy
$terms = ($lang != 'it') ? get_the_terms(get_the_ID(), 'intro_' . $lang) : get_the_terms(get_the_ID(), 'intro');

// Check if terms are available
if ($terms && !is_wp_error($terms)) {
    // Get the first term
    $term = reset($terms);
    // Get the translated term name
    $term_name = $term->name;
} else {
    $term_name = ''; // Handle case where terms are not available
}

// Output the term name
echo $term_name;
1
  • it works for italian, but when I switch the site into german the layout is broken Commented Apr 4 at 16:04

Your Answer

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

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