0

i have made multi-language site thanks to polylang now i dont want to use polylang's switcher as it causes layout issues so i found a better solution is by putting a link on top. for example if user clicks: <a href="mysite.com/fr" ...> whole website gets translated with no issues but it doesn't remember if the user went to another page for example categories page instead the language go back to default which is english unless adding 'fr' to the link manually in the browser.

my thoughts about this is to edit the base URL if the user clicks on the link but have no idea on how to allow regular user to do it or there might be a better different way

any help would be much appreciable. Thanks

1
  • This is extremely painful to achieve, as far as I know. I'll assume the layout breaks due to longer text. Well, if you have such issues, then these issues must be dealt with. Use text-wrap and white-space and read on wrapping text.
    – coolpasta
    Commented Jun 3, 2018 at 2:33

1 Answer 1

0

According to the documentation there are 3 Polylang functions that can help you here :

The first to "remember" the user language.

pll_current_language

// Returns the current language

pll_current_language( $value );

// ‘$value’ => (optional) either ‘name’ or ‘locale’ or ‘slug’, defaults to ‘slug’

returns either the full name, or the WordPress locale (just as the WordPress core function ‘get_locale’ or the slug ( 2-letters code) of the current language.

Now this one to check if the user language exist for the page he clicked

pll_get_post_translations

// Returns an associative array of translations with language code as key and translation post_id as value
pll_get_post_translations( $post_id );
// ‘$post_id’ => (required) id of the post for which you want to get the translations

Now if the user language have been found get the translated post

pll_get_post

pll_get_post( $post_id, $slug );

// ‘$post_id’ => (required) id of the post you want the translation
// ‘$slug’ => (optional) 2-letters code of the language, defaults to current language

For the last one $slug isn't optional for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.