5

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!

-1

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.

7

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

  • 3
    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 '16 at 12:10
5

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' );
3

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;
 }
0

Thank you everyone for these suggestions ! I wanted to set "Elementor canvas" template by default on post only and I did like that :

function default_post_template_elementor_canvas($post_type, $post) 
{
    $wishedTemplate = 'elementor_canvas'; // to see available template var_dump(get_page_templates($post))
    if ($post_type === 'post'
            && in_array($wishedTemplate, get_page_templates($post)) // Only if elementor_canvas is available
            && $post->ID != get_option('page_for_posts') // Not the page for listing posts
            && metadata_exists('post', $post->ID, '_wp_page_template') === false) {  // Only when meta _wp_page_template is not set
      add_post_meta($post->ID, '_wp_page_template', $wishedTemplate);
    }
}
add_action('add_meta_boxes', 'default_post_template_elementor_canvas', 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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