我已经做了一个网站,有2个WordPress安装,一个为英语,一个为爱尔兰语言。它们是相同的设置,具有相同的类别、页面名称等。
我在每个页面的标题中都有‘英语|爱尔兰’链接。
当你在英文页面,你点击顶部的‘爱尔兰’链接,我希望它带你到同一页,但在爱尔兰网站。
链接结构如下图所示:
http://mysite.com/english/about
http://mysite.com/irish/about
所以我真的只需要在网址中的‘英语’替换为‘爱尔兰’
发布于 2012-02-20 18:45:47
它们是标准的wordpress插件,可以为你处理多语言问题。但是,如果你想留下来,选择这个脚本,完全符合你的要求。
$url = 'http://www.mysite.com/english/about/me/test';
$parsedUrl = parse_url($url);
$path_parts = explode("/",$parsedUrl[path]);
$newUrl = $parsedUrl[scheme] . "://" . $parsedUrl[host];
foreach($path_parts as $key =>$part){
if($key == "1"){
if($part == "english") $newUrl .= "/irish";
else $newUrl .= "/english";
} elseif($key > "1"){
$newUrl .= "/" . $part;
}
}
echo "Old: ". $url . "<br />New: " .$newUrl;发布于 2012-02-20 18:39:28
您是否在使用本地化-请参阅http://codex.wordpress.org/I18n_for_WordPress_Developers和http://codex.wordpress.org/Multilingual_WordPress?如果是,请参阅http://codex.wordpress.org/Function_Reference/get_locale。您可以使用它来检测区域设置并相应地更新链接。如果你使用的是插件,你应该查看插件文档。
如果不是,您可以解析当前的URL并分解路径,然后以这种方式更新链接- http://php.net/manual/en/function.parse-url.php
示例:
<?php
$url = 'http://www.domain-name.com/english/index.php/tag/my-tag';
$path = parse_url($url);
// split the path
$parts = explode('/', $path[path]);
//get the first item
$tag = $parts[1];
print "First path element: " . $tag . "\n";
$newPath = "";
//creating a default switch statement catches (the unlikely event of) unknown cases so our links don't break
switch ($tag) {
case "english":
$newPath = "irish";
break;
default:
$newPath = "english";
}
print "New path element to include: " . $newPath . "\n";
//you could actually just use $parts, but I though this might be easier to read
$pathSuffix = $parts;
unset($pathSuffix[0],$pathSuffix[1]);
//now get the start of the url and construct a new url
$newUrl = $path[scheme] . "://" . $path[host] . "/" . $newPath . "/" . implode("/",$pathSuffix) . "\n";
//full credit to the post below for the first bit ;)
print "Old url: " . $url . "\n". "New url: " . $newUrl;
?>改编自http://www.codingforums.com/archive/index.php/t-186104.html
https://stackoverflow.com/questions/9359504
复制相似问题