If you use the Polylang plugin for language mutations in WordPress and you need to fit their switching within the template, the following solution may be useful.
Default Polylang function
Polylang offers a pll_the_languages () function that allows you to switch language mutations. Unfortunately, there aren’t many ways to influence the resulting HTML, and if you need to fit language switching directly into your already finished template, it’s a problem and you need to reach for your own solution.
Get the current language in Polylang
Use the pll_current_language () function in Polylang to get the active language mutation. However, it only returns the language code, such as cs for Czech. Sometimes it can be useful to display the full name, for example, in some cases we may need a URL mutation.
In the following snippet, we’ll use the pll_the_languages () function, which has already been discussed, but we will “process” its output in our own way.
Thanks to these 3 lines of code, we get a field with the necessary data for all language versions in the $ languages variable and store the data for the current version in $ current .
For each mutation, we have data that can be freely used:
Bootstrap dropdown – language selection
In the code sample below, it’s all embedded in the HTML structure of Bootstrap dropdown , so if your template is running on Bootstrap, you should see a drop-down menu to switch languages .
Leave a Reply