admin管理员组

文章数量:1122846

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?

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?

Share Improve this question asked Apr 3, 2024 at 18:54 Andrea SacconiAndrea Sacconi 797 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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' );

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;

本文标签: phptranslating a custom taxonomy term in a shortcode