0

The problem: All languages that have published content show up in the language switcher which is situated in the main navigation bar. As I only have a few pages in Swedish and users are directed directly to them, I would like to remove the link to the Swedish language translation from the main navigation.

Solution: After searching forums and reading posts here on Stack Overflow, it seems that the best way to go (if I've understood correctly) is to create a function in the theme functions.php file and add a filter to the switcher that removes Swedish if found. This is also where any wise words would be appreciated; how to go about creating an appropriate filter?

This is what I have so far. However, my site crashes when I try it out.

function trim_language_switcher() {
    $del_val = 'sv'; // Value to be deleted 
    if(function_exists('pll_the_languages')) { 
        $languages = pll_the_languages(array('raw'=>) // Get raw data as array  
        if (($key = array_search($del_val, $languages)) !== false) { // Get key for value
            unset($languages[$key]);
        }
        return $languages; 
    }
}
add_filter( 'pll_the_languages', 'trim_language_switcher' );

Any help is greatly appreciated!

TBA
  • 1,921
  • 4
  • 13
  • 26
AB42
  • 1
  • 2

1 Answers1

0

Your $languages variable is not correctly set and there is also a typo (missing ")"). You should do this instead:

$languages = pll_the_languages( array( 'raw'=> 1 ) );

But here you are trying to call the pll_the_languages() function inside the filter pll_the_languages which is applied by the previous function. In the documentation you can see that the filter will pass two arguments to your hooked function trim_language_switcher(). To remove Swedish links you have to parse the $output parameter and remove the links before returning it.

Hugo-D
  • 71
  • 4