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 would like to show all articles on my page in two languages while the menu and everything is translated in the language choosen.

<?php 
$id=5; $post = get_page($id); 
$content = qtrans_use('fr', $post->post_content,false); 
echo $content;  
?>

This is as far as I got. But I want it to be dynamic with every article.

Thanks for your help!

share|improve this question

closed as off-topic by Mark Kaplun, Pieter Goosen, birgire, mrwweb, ialocin Sep 20 at 12:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – Mark Kaplun, Pieter Goosen, birgire, mrwweb, ialocin
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

If you go by that route, you might easily break any other plugin that does stuff with the post content, like custom galleries, excerpt plugins, and such. I think there is a less destructive way to achieve what you (we) want.

When a post is about to be displayed, the function qtrans_useCurrentLanguageIfNotFoundShowAvailable is called by qTranslate, attached as a filter on the the_content hook.

Since wordpress themes are executed after plugins (I assume you are writing a theme and not a plugin), you can replace that hook with one of your own, outputting the languages you want.

You can do something like this (in your functions.php file)

// Remove qTranslate default display hook.
// If this code breaks, update the parameters according to
// the qtranslate_hooks.php file of the plugin.
remove_filter('the_content', 'qtrans_useCurrentLanguageIfNotFoundShowAvailable', 0);

// Add a custom filter for the content
add_filter('the_content', 'qtrans_useLanguagesIDecide', 0);
function qtrans_useLanguagesIDecide($content) {
  $YOUR_LANGUAGES = array('en', 'fr');
  $output = '';
  foreach($YOUR_LANGUAGES as $language)
    $output .=
        '<div class="language-'.$language.'">'.
          qtrans_use($language, $content, true).
        '</div>';
  return $output;
}

Note also that there are other hooks you might like to similarly override, like the_excerpt or the_content_rss.

If your blog might not have all the languages for every post (like mine), you might also want to improve your filter, to avoid that the message "Sorry, this entry is only available in Française" in place of the occasional missing english translation:

function qtrans_useLanguagesIDecide($content) {
  $YOUR_LANGUAGES = array('en', 'fr');

  $show_languages = $YOUR_LANGUAGES;
  $qtranslate_languages = qtrans_split($content); // take a peek at translated content
  foreach($show_languages as $key => $language) {
    $translated_content = isset($qtranslate_languages[$language]) ? trim($qtranslate_languages[$language]) : '';
    if(empty($translated_content))
      // remove missing language
      unset($show_languages[$key]);
  }

  $output = '';
  foreach($show_languages as $language)
    $output .=
        '<div class="language-'.$language.'">'.
          qtrans_use($language, $content, true).
        '</div>';
  return $output;
}

In this case, don't be tempted to use $translated_content directly, as qTranslate does its job better than us.

share|improve this answer

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