To translate custom taxonomies in WPML, you can use the wpml_object_id
function provided by the WPML plugin. This function allows you to retrieve the translated ID of a taxonomy term.
Here’s an example of how you can use the wpml_object_id
function to translate a custom taxonomy term:
$term_id = 123; // ID of the term you want to translate
$taxonomy = 'your_taxonomy'; // Slug of your custom taxonomy
// Get the translated ID of the term
$translated_term_id = wpml_object_id($term_id, $taxonomy, true);
// Get the translated term object
$translated_term = get_term($translated_term_id, $taxonomy);
// Output the translated term name
echo $translated_term->name;
In the example above, we first define the ID of the term we want to translate and the slug of our custom taxonomy. We then use the wpml_object_id
function to retrieve the translated ID of the term. The third parameter of the function is set to true
to ensure that we get the translated ID even if the current language is different from the default language.
Once we have the translated ID, we can use the get_term
function to retrieve the translated term object. Finally, we can access the translated term name using the $translated_term->name
property.
This code snippet can be useful when you have custom taxonomies in your WordPress site and you want to display the translated terms based on the current language. It allows you to retrieve the translated term ID and access the translated term object for further processing or display.
Examples
Example 1: Translating a Custom Taxonomy Term using WPML
This example demonstrates how to translate a custom taxonomy term using WPML. The wpml_translate_term
function is used to translate the term into the desired language.
$term_id = 123; // ID of the term to be translated
$taxonomy = 'custom_taxonomy'; // Name of the custom taxonomy
$language = 'fr'; // Language code of the translation
$translated_term = wpml_translate_term( $term_id, $taxonomy, $language );
// Output the translated term name
echo $translated_term->name;
In this example, we specify the ID of the term to be translated, the name of the custom taxonomy, and the language code of the translation. The wpml_translate_term
function returns the translated term object, which can be used to access the translated term name.
Example 2: Translating All Terms of a Custom Taxonomy using WPML
This example demonstrates how to translate all terms of a custom taxonomy using WPML. We use the get_terms
function to retrieve all terms of the taxonomy, and then loop through each term to translate it using the wpml_translate_term
function.
$taxonomy = 'custom_taxonomy'; // Name of the custom taxonomy
$language = 'fr'; // Language code of the translation
$terms = get_terms( $taxonomy );
foreach ( $terms as $term ) {
$translated_term = wpml_translate_term( $term->term_id, $taxonomy, $language );
// Output the translated term name
echo $translated_term->name;
}
In this example, we retrieve all terms of the custom taxonomy using the get_terms
function. Then, we loop through each term and translate it using the wpml_translate_term
function. The translated term object is then used to access the translated term name.
Example 3: Translating a Custom Taxonomy Archive Page Title using WPML
This example demonstrates how to translate the title of a custom taxonomy archive page using WPML. We use the single_term_title
function to retrieve the current term’s name, and then translate it using the wpml_translate_string
function.
$taxonomy = 'custom_taxonomy'; // Name of the custom taxonomy
// Get the current term's name
$term_name = single_term_title( '', false );
// Translate the term name
$translated_term_name = wpml_translate_string( 'taxonomy-term-' . $taxonomy . '-' . $term_name, 'Taxonomy Term', $term_name );
// Output the translated term name
echo $translated_term_name;
In this example, we retrieve the current term’s name using the single_term_title
function. Then, we use the wpml_translate_string
function to translate the term name. The translated term name is then outputted.