WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have two page templates and Qtranslate installed.

I want to choose one or another depending the language selected.

Can I do something like this ?

if($q_config['lang'] == 'en'){
// load page-template_en.php
}else{
// load page-template_de.php
}

Any idea ?

Thanks!

share|improve this question
up vote -1 down vote accepted

It is possible using the template_redirect hook.

Looks something like this :

 function language_redirect()
 {
      global $q_config;

      if( $q_config['lang'] == 'en' )
      {
           include( get_template_directory() . '/page-template_en.php' );
           exit;
      }
      else
      {
           include( get_template_directory() . '/page-template_de.php' );
           exit;
      }
 }
 add_action( 'template_redirect', 'language_redirect' );

Code is untested, but should look like that.

See my similar answer HERE for more help.

share|improve this answer
3  
Don’t use template_redirect to load an alternative template file markjaquith.wordpress.com/2014/02/19/… – Brad Dalton May 2 '14 at 7:47
    
The exit will cause any subsequent script not to run. Use template_include, not template_redirect. – Landing on Jupiter May 18 at 12:12

Finally found it! If I understand your question right, the template is basically saved as metadata that needs to be updated.

update_post_meta( $post_id, '_wp_page_template', 'your_custom_template' );
// or
update_metadata('post_type',  $post_id, '_wp_page_template', 'your_custom_template' );

Source and further info

share|improve this answer
    
This would store a permanent change to the page in the database. But, he needs to change the page dynamically, based on the user's language. – Landing on Jupiter May 18 at 12:10

The best (canonical) way is use template_include hook: http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include

Example code:

function language_redirect($template) {
    global $q_config;
    $new_template = locate_template( array( 'page-'.$q_config['lang'].'.php' ) );
    if ( '' != $new_template ) {
        return $new_template ;
    }
    return $template;
}
add_action( 'template_include', 'language_redirect' );
share|improve this answer

Should be possible using the template_include hook. Code is untested:

 add_action( 'template_include', 'language_redirect' );

 function language_redirect( $template ) {
      global $q_config;
      $lang = ( 'en' === $q_config['lang'] ) ? 'en' : 'de';

      $template = str_replace( '.php', '_'.$lang.'.php', $template );
      return $template;
 }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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