This is How to Get a List of Page Translations in WordPress with Polylang
As a WordPress developer, you may often find yourself working on multilingual websites that use the Polylang plugin to manage translations. One common task you might encounter is the need to retrieve a list of page translations for a specific page.
In this article, we'll explore a solution to this problem and provide you with the code to get the job done.
Understanding the Polylang API
Polylang is a powerful WordPress plugin that allows you to create and manage multilingual content on your website. It provides a comprehensive set of features and functions that you can leverage in your code.
One of the key functions in the Polylang API is pll_get_post()
, which allows you to retrieve the ID of a post (or page) in a specific language. This function takes two parameters:
$post_id
: The ID of the post or page you want to retrieve.
$language
: The language code of the desired translation.
For example, if you have a page with the ID 123
in your default language, and you want to retrieve the ID of the same page in the French translation, you can use the following code:
$french_page_id = pll_get_post(123, 'fr');
This will return the ID of the French translation of the page with the ID 123
.
Getting a List of Page Translations
Now that we understand the basic pll_get_post()
function, let's look at how we can use it to get a list of page translations.
Suppose you have a page on your website, and you want to retrieve the IDs of all its translations. Here's the step-by-step process:
-
Get the current page ID: First, you'll need to get the ID of the current page. You can do this using the get_the_ID()
function:
$current_page_id = get_the_ID();
-
Get the default language: Next, you'll need to determine the default language of your website. You can do this using the pll_default_language()
function:
$default_language = pll_default_language();
-
Get the original page ID: Now, you can use the pll_get_post()
function to retrieve the ID of the page in the default language:
$original_page_id = pll_get_post($current_page_id, $default_language);
-
Get a list of available languages: To get a list of all the available languages on your website, you can use the pll_languages_list()
function:
$available_languages = pll_languages_list();
This will return an array of language codes, such as ['en', 'fr', 'de']
.
-
Loop through the available languages: Finally, you can loop through the available languages and use the pll_get_post()
function to retrieve the IDs of the translated pages:
$translations = [];
foreach ($available_languages as $lang) {
if ($lang !== $default_language) {
$translation_id = pll_get_post($original_page_id, $lang);
if ($translation_id) {
$translations[$lang] = $translation_id;
}
}
}
This will create an associative array $translations
that looks something like this:
[
'fr' => 456,
'de' => 789,
'es' => 321
]
Each key in the array represents a language code, and the value is the ID of the translated page in that language.
Here's the complete code snippet:
$current_page_id = get_the_ID();
$default_language = pll_default_language();
$original_page_id = pll_get_post($current_page_id, $default_language);
$available_languages = pll_languages_list();
$translations = [];
foreach ($available_languages as $lang) {
if ($lang !== $default_language) {
$translation_id = pll_get_post($original_page_id, $lang);
if ($translation_id) {
$translations[$lang] = $translation_id;
}
}
}
// Now you can use the $translations array to perform further operations
This code will give you a list of all the page translations, with the language codes as the keys and the corresponding page IDs as the values.
Real-World Example: Displaying a Multilingual Navigation Menu
Now that you have the list of page translations, you can use this information to display a multilingual navigation menu on your website. Here's an example of how you might do this:
// Get the current page ID and default language
$current_page_id = get_the_ID();
$default_language = pll_default_language();
// Get the list of page translations
$translations = [];
foreach (pll_languages_list() as $lang) {
if ($lang !== $default_language) {
$translation_id = pll_get_post($current_page_id, $lang);
if ($translation_id) {
$translations[$lang] = $translation_id;
}
}
}
// Display the navigation menu
echo '<ul class="nav">';
echo '<li><a href="' . get_permalink($current_page_id) . '">' . get_the_title($current_page_id) . '</a></li>';
foreach ($translations as $lang => $translation_id) {
echo '<li><a href="' . get_permalink($translation_id) . '">' . pll_translate_string(get_the_title($translation_id), $lang) . '</a></li>';
}
echo '</ul>';
In this example, we first get the current page ID and the default language. Then, we use the code from the previous section to get the list of page translations.
Finally, we display the navigation menu, starting with a link to the current page in the default language, followed by links to the translated pages in the other languages. We use the pll_translate_string()
function to ensure that the page titles are displayed in the correct language.
This is just one example of how you can use the list of page translations to enhance your multilingual WordPress website. You can use this information to create custom language switchers, multilingual breadcrumbs, and more.
Conclusion
In this article, we've explored how to use the Polylang API to get a list of page translations in a WordPress website. By following the steps outlined in this guide, you can easily retrieve the IDs of the translated pages and use this information to improve the user experience and functionality of your multilingual site.
Remember, the key to success with Polylang (and any other WordPress plugin) is to understand the available functions and how to leverage them in your code. With a little practice and experimentation, you'll be a Polylang pro in no time!
For more information on Polylang and multilingual WordPress development, be sure to check out the Flowpoint.ai blog, where we regularly publish articles and tutorials on a wide range of WordPress-related topics
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.