WordPress Multilingual Plugin Polylang Theme Function Reference
重要
:使用一个函数之前,你必须Check that the function exists, otherwise your site may encounter fatal errors before Polylang is updated (because WordPress removes the plugin before updating it)
pll_the_languages, display language switcher
使用方法:
pll_the_languages($args).;
$args is an optional parameter with the following options:
- ‘dropdown’ => show as list set to 0, dropdown set to 1 (default: 0)
- ‘show_names’ => show language names set to 1 (default: 1)
- ‘display_names_as’ => can be ‘name’ or ‘slug’ (default: ‘ name’)
- ‘show_flags’ => show flags set to 1 (default: 0)
- ‘hide_if_empty’ => set to 1 to hide the language switcher (or page) when there is no translation (default: 1)
- ‘force_home’ => if set to 1, force link to home page (default: 0)
‘echo’ => display set to 1, return set to 0 (default: 1) - ‘hide_if_no_translation’ => if set to 1, hide the language if there is no translation (default: 0)
- ‘hide_current’=> if set to 1, hide current language (default: 0)
- ‘post_id’ => if set, show a link to the translated post (or page) (default: null)
- ‘raw’ => used to create custom language switcher (default:0)
Important: If you use the language switcher by dropdown, you have to provide your own Action to attach to the language switcher, if you want the same content provided by the widget, you can find the correct code in polylang/include/widget.php, if you don't use the dropdown option, you have to customize the input ul tag.
1,'show_names'=>0)); ? >
1)); ? >
If the above options are not enough, you can also is to customize the language switcher with the ‘raw’ parameter.
$translations = pll_the_languages(array('raw'=>1));
此函数将赴会一个多元数组,每个语言都有以下内容。
- [id] => language id
- [slug] => language code used in urls
- [name] => language name
- [url] => url for translation
- [flag] => url of language flag
- [current_lang] => true if the current language is displayed, otherwise false
- [no_translation] => true if no translation is available, otherwise false
pll_current_language, return current language
使用方法:
pll_current_language($value).;
- ‘$value’ => (optional) can be ‘name’ or ‘locale’ or ‘ slug’, defaults to ‘slug’.’
Returns the full name of the current language, or the WordPress locale, (similar to the WordPress core function ‘get_locale’), or the slug (2-letter language code) of the current language
pll_default_language, returns the default language
使用方法:
pll_default_language($value).;
- ‘$value’ => (optional) can be ‘name’ or ‘locale’ or ‘ slug’, defaults to ‘slug’.’
Returns the full name of the current language, or the WordPress locale, (similar to the WordPress core function ‘get_locale’), or the slug (2-letter language code) of the current language
pll_get_post, which returns the translation of the article (or page)
This function gets the article ID of the current language corresponding to the specified article (note: not the article object), which may be a bit roundabout to understand, for example, the page ID of the Chinese language version of our page is “100”, and the page ID of the English language version of our page is “101 For example, if the ID of the page about our Chinese language is ”100", and the ID of the page about our English language version is "101", this function is to get the IDs of these two pages. The following function in the$post_id parameter is “100”, in the Chinese language, the return is still “100”, in the English language, the return of “101”, the same way, we put the$post_id Setting it to “101” has the same effect.
使用方法:
pll_get_post($post_id, $slug).;
- ‘$post_id’ => (required) id of post to be translated
- ‘$slug’ => (optional) 22-letter language code, defaults to current language
根据获取某个文章各种语言版本的对象
Sometimes, we need to get the content of a post based on a fixed post ID, but if we use WordPress's get_post Polylang of course takes this into account, and uses the following method to get the current language of an article or the corresponding translation of the article.
<?php $post = get_post( pll_get_post( 97, pll_current_language() ) ); ?>
pll_get_term, which returns the translation of the article (or page)
使用方法:
pll_get_post($post_id, $slug).;
- ‘$post_id’ => (required) id of the category to be translated
- ‘$slug’ => (optional) 22-letter language code, defaults to current language
Returns the translated category id as an integer.
pll_home_url, return home URL
使用方法:
pll_home_url($slug).;
- ‘$slug’ => (optional) 22-letter language code, this parameter is optional and defaults to the current language if called in the previous paragraph.
Returns the URL of the currently requested language as a string.
How do I set the navigation to show a language switcher. I've looked many times. I can't find where to set it, can you help me remotely?
Add the code where the language switcher needs to be displayed:
very nice