2012-02-20 101 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