Add Language Switcher in Posts Page Automatically by WordPress plugin Polylang

If you are multi-language IT Pro you may want to show articles in multiple languages just like me. I use Polylang in my blog to switch English and Chinese version. I used to add “Language Switcher” tag in my articles. It’s manual process that I had to remember the tag in somewhere, copy and past it to articles when I was writing. It’s frustration to repeat it in every article. Another problem was I have to add “Read More” tag in each article before I add “Language Switcher” tag. That’s because each post shows a switcher in home page if I don’t do that. It confuses Polylang which URL each article should goes. I want to find a perfect way to fix this problem! Here we go…

To show switcher in each article page but not home page, I need to figure out a way to call the plugin in article template. The template is “content-single.php“. It won’t work if you put tag directly in template file. Thanks WordPress published a function “the_widget()“. It calls plugin according to input parameters.  There are 3 input parameters in the function: $widget, $instance and $args.

$widget is PHP class name of the plugin. I went through all codes of Polylang and eventually found the PHP class name was “PLL_Widget_Languages“. I found it in “polylang/include/widget-languages.php“. I also wanted to hidden current language in article. There is a option “Hides the current language” in the plugin admin page. But it’s not a global setting. I have to figure out which variable it is and pass it to the_widget() function. According to the code, it’s “the_widget( ‘PLL_Widget_Languages’,’hide_current=1′ )”. It’s part of $instance. The last parameter $args is something contains HTML code. I leave it default.

Summarize it, you just need add the_widget( ‘PLL_Widget_Languages’,’hide_current=1′ ) somewhere in content-single.php. 🙂

2 thoughts on “Add Language Switcher in Posts Page Automatically by WordPress plugin Polylang”

  1. Hello guy,
    thank you for this usefull post.

    I’m wondering one thing.
    I’d like my theme to display automatically Polylang widget in my custom sidebar when you install it.

    So I know this is possible for standard WordPress Widgets using “the_widget()” function.
    Example here :

    I guess if I want to call Polylang widget the same way the answer is “the_widget(‘PLL_Widget_Languages’)” ?

    What do you think about it ?

    Thank you

Leave a Reply