Monday, October 14, 2013

Wordpress: Setting a different home page when using Polylang

I have a hungarian website where the index page is of the page type. I recently needed to add more languages to it and faced the problem of where to set the alternative language index pages. In the admin panel I can only set one page, so I started searching for an easy solution that doesn't require the change of page.php to much.

As it turns out, all I had to do is to insert this code before the default  <?php if (have_posts()) : while (have_posts()) : the_post(); ?> line:

 <?php  
      if(get_the_ID() == '') {  
           if(pll_current_language() == 'en') {  
                $wp_query = new WP_Query( 'page_id=166' );  
           };  
      };  
 ?>  

When I go too the english index page (www.example.com/en), the ID is empty, so I check the language and modify it to open my designated english page.

I'm sure, there are better ways to do this, but right now it will do fine.