简体   繁体   中英

WordPress : Treat value in functions.php for all inner PHP files

On my website, I use Polylang, but on each PHP files of my template, I have to make things with a value like Italian = 1, French = 2, default value (english) = 0

At first, I had the following condition on each PHP file :

if (get_locale() == 'it') {
  $lang = 1;
} elseif (get_locale() == 'fr') {
  $lang = 2;
} {
  $lang = 0;
}

It worked very well, but I had to add this code on each PHP file of my website, hard... I was looking for a solution with the functions.php file.

I tried :

function check_lang() {
  if (get_locale() == 'it') {
    $lang = 1;
  } elseif (get_locale() == 'fr') {
    $lang = 2;
  } {
    $lang = 0;
  }
}
add_action( 'init', 'check_lang' );

But on some files, it doesn't work, the value of $lang is "it" instead of "1"...

Do I made a mistake on my code ?

How are you trying to use the $lang variable? In your check_lang() function you're setting it to an integer but you're not doing anything with it. At the very least, you need a return $lang statement in the function but you also need additional code elsewhere to make use of the returned value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM