A couple of months ago, I showed how to use the Polylang language plugin with Customizr.
Polylang is easy to set up and use. It even has an option for you to include flags or language names (or both), directly to the menu, with no coding.
However, it doesn’t give you an option to put a dropdown in the menu/header area. This snippet shows you how to do this, using a navbar widget
Setting up a new navbar widget
By default, the Polylang Language Switcher—which gives you a “dropdown” option—is set up to be dragged inside a widget area. Customizr’s widgets (like in many other themes) are in the footer and in the sidebars. You may not want sidebars on your pages. And in any case, with Customizr’s front page, sidebars are not even shown at the top of the page.
If you need the Language Switcher to be at the top so that users can see it easily and switch immediately to their language, what can you do?
Well, it turns out that defining your own widget area is actually quite easy. I showed you how to add one after the header here. This time we’ll add a widget area after the navbar, where you can place the Language Switcher dropdown.
To define a new widget area, add the following code to the functions.php file in your child theme:
// Adds a widget area to house a Polylang dropdown. See also accompanying css. if (function_exists('register_sidebar')) { register_sidebar(array( 'name' => 'Extra Widget After Navbar', 'id' => 'extra-widget', 'description' => 'Extra widget after the navbar', 'before_widget' => '<div class="widget %2$s" id="%1$s">', 'after_widget' => '</div>', 'before_title' => '<h2>', 'after_title' => '</h2>' )); }
More about the widgets API in WordPress : http://codex.wordpress.org/Widgets_API
Where to copy/paste this code? => in your functions.php file. We strongly recommend you create a child theme. Download a start-up child theme here.
Remember: you shouldn’t edit the theme’s functions.php, nor should you put a copy of it into your child theme.
Now you’ve set up the widget area, you’ll need to place it on your page. This is where Customizr’s flexibility comes in, as it has “hooks” throughout the code, on which you can “hook” your code. Add the following code to the code above:
// Place the widget area after the navbar add_filter ('__after_navbar', 'add_my_widget'); function add_my_widget() { if (function_exists('dynamic_sidebar')) { dynamic_sidebar('Extra Widget After Navbar'); } }
That’s your new widget area set up. When you go to Appearance > Widgets, you’ll find your widget area there, ready to be filled with the Polylang Language Switcher.
Adding the Polylang Language Switcher to the widget
Go to Appearance > Widgets and do the following:
- Drag the Polylang Language Switcher over to your new widget area (which you called “Extra Widget After Navbar”)
- Select “Displays as dropdown”
- Click “Save”.
Styling the dropdown
You can now style the dropdown. The CSS below will move the dropdown inside the navbar on the right of the page. Add these CSS rules to your child theme’s style.css:
/* Add dropdown to navbar, see also accompanying php */ #lang_choice { position: absolute; right: 20px; top: 90px; width: 80px; z-index: 250; } @media (max-width: 979px) { /* Shift dropdown down for responsive navbar*/ #lang_choice { /* Change this, depending on your site */ top: 130px; } } @media (max-width: 480px) { /* Shift dropdown for smaller screens*/ #lang_choice { /* Change this, depending on your site */ top: 130px; } }
You may need to tweak some of these values, depending on any other changes you’ve made / the length of your tagline / size of your logo etc.
8 thoughts on “Add a Polylang dropdown with a navbar widget”
Hello,
i would add this Class “Das Bad ist geöffnet” to the navbar. how is it possible?
Hi,
You can hook into the tc_navbar_wrapper_class filter and add the classes
Hi
Firstly, thanks for creating and maintaining this theme. I know only very basic HTML and CSS and no PHP but have been able to muddle along mostly fine with this great theme.
I’ve added an extra widget area for the polylang switcher as in this post and it works great (the site is at http://ied.suwon.ac.kr/). I’d like to also add some extra widgets (a text widget with an inline menu to a few links and maybe also a search box) but when I do, they display vertically. Can you please let me know how how I can make only widgets in this new widget area display horizonally?
Many thanks.
Thanks for this great snippet
I use this snippet to place the selector above my navbar. It worked well untill the last update of Polylang.
To make it work again, change in the css file : #lang_choice–> #lang_choice_polylang-2
new code is
Fantastic! Thanks for this contribution 😀
Works really well in chrome but too much discrepancy in explorer and firefox (both the same) to iron out with css.
Hi Chris. Everyone’s site modifications are different. The size of the logo, the length of the tagline etc. will all make a difference to the positioning, which is why I said above that the CSS will need to be tweaked.
We can help you out with the CSS over on the WordPress Customizr support forum, or you can instead use Polylang’s feature to add the languages to the menu.