Order polylang menu with currently active language always on top

Published on September 11th, 2015

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();

Leave a Reply

Your email address will not be published. Required fields are marked *