Polylang Theme Function Reference for WordPress Multilingual Development

Important

Before using any function, you must check whether that function exists. Otherwise your site may run into a fatal error during a Polylang update, because WordPress deletes the existing plugin files before copying in the new version.

pll_the_languages: Display the language switcher

Usage:

pll_the_languages($args);

$args is an optional parameter. The available options are:

  • 'dropdown' => set to 0 to display a list, or 1 to display a dropdown (default: 0)
  • 'show_names' => set to 1 to display language names (default: 1)
  • 'display_names_as' => can be 'name' or 'slug' (default: 'name')
  • 'show_flags' => set to 1 to display flags (default: 0)
  • 'hide_if_empty' => set to 1 to hide the switcher or the page when there is no translation (default: 1)
  • 'force_home' => if set to 1, force links to the homepage (default: 0)
    'echo' => set to 1 to echo the output, or 0 to return it instead (default: 1)
  • 'hide_if_no_translation' => if set to 1, hide the language when no translation exists (default: 0)
  • 'hide_current' => if set to 1, hide the current language (default: 0)
  • 'post_id' => if set, display links pointing to the translated post or page (default: null)
  • 'raw' => used when building a custom language switcher (default: 0)

Important: if you display the language switcher as a dropdown, you must provide the additional action behavior for the switcher yourself. If you want the same output as the widget provides, you can find the correct code in polylang/include/widget.php. If you do not use the dropdown format, you must output the surrounding ul tag yourself.

<?php // Output a list of language names. ?> <ul><?php pll_the_languages(); ?></ul>
<?php // Output a list of flags only, without language names. ?> <ul>
<?php pll_the_languages(array('show_flags'=>1,'show_names'=>0)); ?></ul>
<?php // Output the language list as a dropdown. ?> <?php pll_the_languages(array('dropdown'=>1)); ?>

If the options above are still not enough, you can also use the 'raw' parameter to build a completely custom language switcher.

$translations = pll_the_languages(array('raw'=>1));

This function returns a multidimensional array, and each language includes the following values.

  • [id] => the language ID
  • [slug] => the language code used in URLs
  • [name] => the language name
  • [url] => the translated URL
  • [flag] => the URL of the language flag image
  • [current_lang] => true if it is the current language, otherwise false
  • [no_translation] => true if no translation is available, otherwise false

pll_current_language: Return the current language

Usage:

pll_current_language($value);
  • $value => (optional) can be 'name', 'locale', or 'slug'; the default is 'slug'

It returns the full name of the current language, the WordPress locale (similar to WordPress core’s get_locale), or the slug of the current language, which is the two-letter language code.

pll_default_language: Return the default language

Usage:

pll_default_language($value);
  • $value => (optional) can be 'name', 'locale', or 'slug'; the default is 'slug'

It returns the full name of the default language, the WordPress locale, or the slug of the default language, which is the two-letter language code.

pll_get_post: Return the translation of a post or page

This function returns the post ID of the current-language translation that corresponds to the specified post. Note that it returns the translated post ID, not the post object itself. For example, assume the Chinese version of an About page has the ID 100 and the English translation has the ID 101. This function is used to retrieve those IDs. If the $post_id parameter below is 100, then in the Chinese language context it returns 100, and in the English language context it returns 101. Likewise, setting $post_id to 101 has the same effect in reverse.

Usage:

pll_get_post($post_id, $slug);
  • $post_id => (required) the ID of the post that needs translation lookup
  • $slug => (optional) the two-letter language code; defaults to the current language

Get the translated post object for the current language based on a fixed post ID

Sometimes we need to fetch the content of a specific article based on a fixed post ID. If we use WordPress’s get_post function directly, we only get that one fixed post, so the page content does not change when the language is switched. Polylang naturally takes this into account. With the following method, you can get the current-language version, or the corresponding translation, of a given post.

<?php $post = get_post( pll_get_post( 97, pll_current_language() ) ); ?>

pll_get_term: Return the translation of a term

Usage:

pll_get_post($post_id, $slug);
  • $post_id => (required) the ID of the category or term that needs translation lookup
  • $slug => (optional) the two-letter language code; defaults to the current language

It returns the translated term ID as an integer.

pll_home_url: Return the homepage URL

Usage:

pll_home_url($slug);
  • $slug => (optional) the two-letter language code. This parameter is optional, and when called on the front end it defaults to the current language.

It returns the homepage URL of the current request language as a string.

This helper is typically used when you need to output the correct homepage link for the active Polylang language inside a theme template.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *