• Resolved salemmarafi

    (@salemmarafi)


    I am trying to do the following:

    Rewrite the URL structure of my WordPress installation so that a language field is in there. E.g. https://www.mydomain.com/lang/

    I want to then take the input from /lang/ and use it to display the appropriate content. E.g. if lang is ‘en’ I will take custom fields in English and display the theme in English.

    Here is what I have so far:

    <?php
     function add_directory_rewrite() {
    	global $wp_rewrite;
    	add_rewrite_tag('%lang%', '(.+)');
            add_rewrite_rule('^/(.+)/', 'index.php?p=$matches[1]&lang=$matches[2]', 'top');
    	add_permastruct('lang', '%lang%');
    
       }
    add_action( 'init', 'add_directory_rewrite' );
    ?>

    This works as far as getting the language but the problem I am facing is now the_permalink() has “/%lang%/” where /en/ is supposed to be or /fr/ or /de/ or whatever language. To add more detail my permalink structure is /%lang%/%category%/%postname%/ and lets say I have a category called food and a post with the title chicken, the_permalink will generate https://www.mydomain.com/%lang%/food/chicken/

    Any idea what I’m doing wrong? Cheers.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Language Switch’ is closed to new replies.