最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Add language prefix to permalinks

programmeradmin0浏览0评论

I have a wordpress website that has multiple translations, and i was trying to get the website to determine the language to load from the URL e.g. if its , load contents of , but in spanish.

I have tried something like this,

add_action('plugins_loaded', 'reroute_url');
function reroute_url(){
    $languages = ['sp'];


    $uri = $_SERVER['REQUEST_URI'];
    $uri_components = explode("/", $uri);
    $language = $uri_components[1];
    if (!in_array($language, $languages)){
        return;
    }
    $_SERVER['REQUEST_URI'] = substr($uri, 1 + strlen($language)).'?language='.$language;
}

basically converting to so then i can use this GET parameter to change the language, but this ended up with wordpress redirecting to . Is there a way to pass this converted url to wordpress, without actually redirecting to this url?

I have a wordpress website that has multiple translations, and i was trying to get the website to determine the language to load from the URL e.g. if its https://website/sp/about-us, load contents of https://website/about-us, but in spanish.

I have tried something like this,

add_action('plugins_loaded', 'reroute_url');
function reroute_url(){
    $languages = ['sp'];


    $uri = $_SERVER['REQUEST_URI'];
    $uri_components = explode("/", $uri);
    $language = $uri_components[1];
    if (!in_array($language, $languages)){
        return;
    }
    $_SERVER['REQUEST_URI'] = substr($uri, 1 + strlen($language)).'?language='.$language;
}

basically converting https://website/sp/about-us to https://website/about-us?language=sp so then i can use this GET parameter to change the language, but this ended up with wordpress redirecting to https://website/about-us?language=sp. Is there a way to pass this converted url to wordpress, without actually redirecting to this url?

Share Improve this question asked Jun 22, 2019 at 13:32 whynoworkwhynowork 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

I'm not sure if this is the "best" way to approach it but this is what I would try first.

I would create a taxonomy (like categories) called languages. Maybe even use categories (if they are not in use for something else). You can structure permalinks to go example/<cat-name>/<page-stub> and, thus, have the language as part of the URL. I would imagine you can do the same for a custom taxonomy.

This would allow you to have separate archives for each language.

You can use register taxonomy to make a new category-like or tag-like taxonomy.

发布评论

评论列表(0)

  1. 暂无评论