2012-02-20 100 views
0

我制作了一个网站,其中一个用于英语,一个用于爱尔兰语。他们是相同的设置,具有相同的类别,页面名称等。在标题中更改WordPress链接

我有'English |爱尔兰'链接在我的每页上的标题。

当您在英文页面上点击顶部的'爱尔兰'链接时,我希望它能带您进入同一页面,但在爱尔兰网站上。

链接结构如下图所示:

http://mysite.com/english/about

http://mysite.com/irish/about

所以我真的只需要 '英语' 在URL中通过 '爱尔兰'

回答

1

适应了他们是标准的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; 
+0

加1用于编写特定代码的麻烦。 – 2012-02-20 10:53:11

+0

其实,一个小小的变化。您需要将路径的其余部分添加到$ newURL的末尾,因为“我希望它将您带到同一页面,但在爱尔兰站点上”。我正在写一个更新到我的地方,我正在做这个。 – 2012-02-20 11:28:31

+0

我已经在 elseif($ key>“1”){newUrl。=“/”)部分做了这些。 $一部分; 如果你运行代码,你会发现它已经是 – Daan 2012-02-20 11:33:27

0

更换是否使用本地化 - 请参阅http://codex.wordpress.org/I18n_for_WordPress_Developershttp://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