'How to remove language link from Polylang language switcher when a language has published content?
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!
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hugo-D |
