0

I’m developing a website where the home page is made from a custom template that I created. Once there, you can enter to the site and view the lastest posts.

The thing is that when I’m viewing the lastest posts and I want to change the site language, it takes me back to the landing page. I know that’s the correct behavior because that’s the home page.

But how can I modify it in order that the language switcher doesn’t take me to the landing page, but instead it only changes the language of the latest post?

Let’s say that the home site URL is: http://example.com/en/home

And the lastest posts URL is: http://example.com/en/

Thanks in advance.

1 Answers1

0

You should try something it like this:

//fetch current URL
$current_url = explode ('/', $_SERVER['REQUEST_URI']);

//change language to 'de'
if ( strlen ( $current_url[1] ) == 2 ) $current_url[1] = 'de';

//new url (after /) will be
$new_url = implode('/', $current_url);

please note, there is only 'simple' check - if your first parameter in URL contains language - length of 2 chars. Only if this will be true, the language will be changed. there is a lot of to do it safe and efficient, but it also depends on your project.

Marian Sabo
  • 106
  • 6