Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

4 Answers 4

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
1  
Don’t use template_redirect to load an alternative template file markjaquith.wordpress.com/2014/02/19/… –  Brad Dalton May 2 at 7:47

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

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.