To translate custom post excerpts using WPML in WordPress, you can use the wpml_translate_string
function provided by the WPML plugin. This function allows you to translate any string, including custom post excerpts, into different languages.
Here’s an example code snippet that demonstrates how to translate a custom post excerpt using WPML:
function wpsnippets_translate_custom_excerpt($excerpt, $post_id) {
// Check if WPML is active
if (function_exists('wpml_translate_string')) {
// Translate the custom excerpt using WPML
$translated_excerpt = wpml_translate_string('post_excerpt', $excerpt, array('post_id' => $post_id));
// Return the translated excerpt
return $translated_excerpt;
}
// If WPML is not active, return the original excerpt
return $excerpt;
}
In this code snippet, we define a custom function wpsnippets_translate_custom_excerpt
that takes two parameters: $excerpt
(the original excerpt) and $post_id
(the ID of the post).
Inside the function, we first check if the WPML plugin is active by using the function_exists
function. If WPML is active, we then use the wpml_translate_string
function to translate the custom excerpt. The first parameter of wpml_translate_string
is the context, which is set to 'post_excerpt'
in this case. The second parameter is the original excerpt, and the third parameter is an array that includes the post ID.
If WPML is not active, we simply return the original excerpt without any translation.
You can use this code snippet in your theme’s functions.php
file or in a custom plugin. After adding the code, you can call the wpsnippets_translate_custom_excerpt
function passing the original excerpt and post ID as arguments to get the translated excerpt.
Examples
Example 1: Translating custom post excerpts using WPML
This use case demonstrates how to translate custom post excerpts using WPML. The code example below shows how to retrieve the translated excerpt for a specific post in the current language.
<?php
$post_id = 123; // Replace with the ID of the post you want to retrieve the translated excerpt for
$language = apply_filters( 'wpml_current_language', NULL ); // Get the current language
$translated_excerpt = apply_filters( 'wpml_translate_single_string', get_the_excerpt( $post_id ), 'Post Excerpts', 'Excerpt for post ' . $post_id, $language );
echo $translated_excerpt;
?>
Explanation:
- The
$post_id
variable should be set to the ID of the post you want to retrieve the translated excerpt for. - The
$language
variable is used to get the current language using thewpml_current_language
filter. - The
wpml_translate_single_string
filter is used to translate the excerpt. It takes the original excerpt, the translation group name, the translation string name, and the target language as parameters. - The translated excerpt is then echoed on the page.
Example 2: Translating custom post excerpts in a loop using WPML
This use case demonstrates how to translate custom post excerpts in a loop using WPML. The code example below shows how to retrieve and display the translated excerpts for multiple posts in the current language.
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
$language = apply_filters( 'wpml_current_language', NULL );
$translated_excerpt = apply_filters( 'wpml_translate_single_string', get_the_excerpt( $post_id ), 'Post Excerpts', 'Excerpt for post ' . $post_id, $language );
echo $translated_excerpt;
}
wp_reset_postdata();
} else {
echo 'No posts found.';
}
?>
Explanation:
- The code uses a
WP_Query
to retrieve a list of posts. You can modify the$args
array to customize the query. - Inside the loop, the post ID is retrieved using
get_the_ID()
. - The current language is obtained using the
wpml_current_language
filter. - The
wpml_translate_single_string
filter is used to translate the excerpt for each post. - The translated excerpt is then echoed on the page.
- Finally,
wp_reset_postdata()
is called to restore the global post data.
Example 3: Translating custom post excerpts using a custom function
This use case demonstrates how to translate custom post excerpts using a custom function with the prefix wpsnippets_
. The code example below shows how to create a custom function that retrieves the translated excerpt for a specific post in the current language.
<?php
function wpsnippets_get_translated_excerpt( $post_id ) {
$language = apply_filters( 'wpml_current_language', NULL );
$translated_excerpt = apply_filters( 'wpml_translate_single_string', get_the_excerpt( $post_id ), 'Post Excerpts', 'Excerpt for post ' . $post_id, $language );
return $translated_excerpt;
}
$post_id = 123; // Replace with the ID of the post you want to retrieve the translated excerpt for
$translated_excerpt = wpsnippets_get_translated_excerpt( $post_id );
echo $translated_excerpt;
?>
Explanation:
- The
wpsnippets_get_translated_excerpt
function is created to encapsulate the logic for retrieving the translated excerpt. - Inside the function, the
$post_id
parameter is used to retrieve the excerpt for the specified post. - The current language is obtained using the
wpml_current_language
filter. - The
wpml_translate_single_string
filter is used to translate the excerpt. - The translated excerpt is then returned by the function.
- Outside the function, the custom function is called with the desired post ID, and the translated excerpt is echoed on the page.