Order polylang menu with currently active language always on top

Polylang is a great WordPress multilanguage plugin. It works very well, but one little thing it doesn’t do out of the box is to give you a list of languages while keeping the currently selected item always on top.

In case you need it, here is how to do it. Add the code below to your functions php.

function sortit($a, $b) {
  if($a['current_lang'] === $b['current_lang']) {
    return 0;
  }
  return $a['current_lang'] > $b['current_lang'] ? -1 : 1;
}

function output_menu(){
  $langs = pll_the_languages(array('raw'=>1));
  uasort($langs, 'sortit');
  foreach ($langs as $lang) {
    $current_lang = ($lang['current_lang'] == true) ? 'current-lang' : '' ;
    echo '

 

 

‘; } }

I’ve also added a “current-lang” class for your convenience 🙂

Then in your theme, call the function as usual

 
output_menu();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s