更改标题中的WordPress链接

时间:2012-02-20 10:19:18

标签: wordpress

我创建了一个网站,其中有2个WordPress安装一个用于英语,一个用于爱尔兰语。它们是相同的设置,具有相同的类别,页面名称等。

我有'英语|每页的标题中都有爱尔兰语链接。

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

链接结构如下所示:

http://mysite.com/english/about

http://mysite.com/irish/about

所以我真的只需要在网址中用'英语'替换为'爱尔兰'

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

您使用的是本地化 - 请参阅http://codex.wordpress.org/I18n_for_WordPress_Developershttp://codex.wordpress.org/Multilingual_WordPress吗?如果是,请参阅http://codex.wordpress.org/Function_Reference/get_locale。您可以使用它来检测区域设置并相应地更新链接。如果您使用的是插件,则应查看插件文档。

如果没有,您可以解析当前网址并展开路径,然后以这种方式更新链接 - 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