Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用自定义代码更改WordPress中的语言_Php_Wordpress_Multilingual - Fatal编程技术网

Php 使用自定义代码更改WordPress中的语言

Php 使用自定义代码更改WordPress中的语言,php,wordpress,multilingual,Php,Wordpress,Multilingual,我在WP中有一个网站,它有多种语言,如(英语、德语和法语)http://example.com(英文)。http://example.com/?lang=fr(法语)和http://example.com/?lang=de(德语) 我试图在页面加载之前更改语言。所以我在index.php上尝试了一些代码,但这会产生问题。 我的代码是: $currentUrl = 'this is current page url'; $store_code = "french"; switch ($store

我在WP中有一个网站,它有多种语言,如
(英语、德语和法语)
http://example.com(英文)。http://example.com/?lang=fr(法语)和http://example.com/?lang=de(德语) 我试图在页面加载之前更改语言。所以我在
index.php
上尝试了一些代码,但这会产生问题。 我的代码是:

$currentUrl = 'this is current page url';
 $store_code = "french";
switch ($store_code) { 
    case 'french':
       $_baseurl = $currentUrl."?lang=fr"; 

        echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;
case 'german':
       $_baseurl = $currentUrl."?lang=de"; 
        echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break; 
case 'mandarin':
       $_baseurl = $currentUrl."?lang=zh"; 
        echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;   
    default:
       $_baseurl = $currentUrl; 
        echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;
}
Output.

它像这样生成
输出
,因为您在switch中只使用了法语,所以每次它都只得到
大小写
。您可以做的是制作该语言的数组,然后在
开关中使用它,如下所示:

    $currentUrl = 'this is current page url';
    $store_code = array('french', 'german', 'mandarin');
    foreach($store_code as $mylang){
        switch ($mylang) { 
            case 'french':
               $_baseurl = $currentUrl."?lang=fr"; 

                echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
                break;
        case 'german':
               $_baseurl = $currentUrl."?lang=de"; 
                echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
                break; 
        case 'mandarin':
               $_baseurl = $currentUrl."?lang=zh"; 
                echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
                break;   
            default:
               $_baseurl = $currentUrl; 
                echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
                break;
        }
    }
$currentUrl='这是当前页面url';
$store_code=数组('法语'、'德语'、'普通话');
foreach($store_代码为$mylang){
开关($mylang){
“法语”一案:
$\u baseurl=$currentUrl。“?lang=fr”;
echo“window.location='{$\u baseurl}';
打破
“德语”一案:
$\u baseurl=$currentUrl。“?lang=de”;
echo“window.location='{$\u baseurl}';
打破
“普通话”一案:
$\u baseurl=$currentUrl。“?lang=zh”;
echo“window.location='{$\u baseurl}';
打破
违约:
$\u baseurl=$currentUrl;
echo“window.location='{$\u baseurl}';
打破
}
}

请让我知道这里的输出。

我在切换状态之前解决了它的使用条件。它工作正常

define('WP_USE_THEMES', true);
require("../app/Mage.php");
umask(0);
Mage::app('default');
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
 $store_code = Mage::app()->getStore()->getCode();
if(Mage::getSingleton('core/session', array('name' => 'frontend'))->getData("foo") !='bar')
{

Mage::getSingleton('core/session')->setData("foo","bar");
switch ($store_code) { 
    case 'french':
       $_baseurl = $currentUrl."?lang=fr"; 
         echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;
case 'german':
       $_baseurl = $currentUrl."?lang=de"; 
         echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break; 
case 'mandarin':
       $_baseurl = $currentUrl."?lang=zh"; 
        echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;   
    default:
       $_baseurl = $currentUrl; 
         echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;
}
}

但它产生了问题
它产生了什么问题?@Rohil_PHPBeginner我在问题中添加了错误谢谢,但法语只是一个例子。比如当页面加载时,我有一个代码,现在我必须在代码的基础上设置条件。希望你理解我想说的问题是它调用法语agian和agian。虽然我认为在第一次尝试后不应该这样做。对不起,但我还是没有理解你。。你在说什么代码?你试过上面的代码吗?它给出了什么?我是从另一个设置代码的页面来到这个页面的。这个代码可能是法语或德语。所以我使用switch语句来检查并加载相应的语言。好的。。那么,从哪个页面开始,您是否传递了任何参数?如果你不介意的话,我也可以看看那页吗?这个解决方案不可取,当$currentUrl已经包含
时,它将导致无效URL。您最好使用
http\u build\u query
来保持可能的其他GET参数不变并创建有效的URL@Dr.Molle现在检查。我认为它现在非常适合在会话基础上进行语言切换。我正在使用Magento和WP。
define('WP_USE_THEMES', true);
require("../app/Mage.php");
umask(0);
Mage::app('default');
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
 $store_code = Mage::app()->getStore()->getCode();
if(Mage::getSingleton('core/session', array('name' => 'frontend'))->getData("foo") !='bar')
{

Mage::getSingleton('core/session')->setData("foo","bar");
switch ($store_code) { 
    case 'french':
       $_baseurl = $currentUrl."?lang=fr"; 
         echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;
case 'german':
       $_baseurl = $currentUrl."?lang=de"; 
         echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break; 
case 'mandarin':
       $_baseurl = $currentUrl."?lang=zh"; 
        echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;   
    default:
       $_baseurl = $currentUrl; 
         echo "<script type='text/javascript'>window.location='{$_baseurl}'</script>";
        break;
}
}
Mage::getSingleton('core/session')->setData("foo","magento");